java – 处理json和form params的Spring MVC方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 处理json和form params的Spring MVC方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3979字,纯文字阅读大概需要6分钟。
内容图文
![java – 处理json和form params的Spring MVC方法](/upload/InfoBanner/zyjiaocheng/826/0fd606ad1d94435fb66cff4092f4f560.jpg)
我想在单弹簧mvc方法中处理内容类型application / x-www-form-urlencoded和application / json.
我在休息服务中要求接受输入作为表单参数或json.我可以通过编写两种方法来实现这一点.无论是形式params还是json,响应将永远是json.
@RequestMapping (method = RequestMethod.POST, produces = {"application/json"},
consumes = {"application/x-www-form-urlencoded"})
public @ResponseBody Book createBook(Book book)
throws Exception {
return book;
}
@RequestMapping (method = RequestMethod.POST, produces = {"application/json"},
consumes = {"application/json"})
public @ResponseBody Book createBookJSON(@RequestBody Book book)
throws Exception {
return book;
}
是否可以将这两种方法合二为一,使其有效?任何帮助都感激不尽.
编辑
我实现了相同的,我的控制器和配置如下,但是当我发送json请求时,我得到null值作为响应.
当我发送表格参数时它工作正常.帮我找出问题所在.
控制器方法
@RequestMapping (method = RequestMethod.POST, produces = {"application/json", "application/xml"}, consumes = {"application/x-www-form-urlencoded", "application/json"})
public @ResponseBody Book createBook(Book book)
throws Exception {
return book;
}
的servlet上下文
<mvc:view-controller path="/" view-name="index"/>
<context:annotation-config />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="mediaTypes">
<map>
<entry key="xml" value="application/xml" />
<entry key="json" value="application/json" />
</map>
</property>
<property name="defaultViews">
<list>
<!-- JSON View -->
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
<!-- JAXB XML View -->
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<ref bean="jaxb2Marshaller" />
</constructor-arg>
</bean>
</list>
</property>
<property name="ignoreAcceptHeader" value="true" />
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<property name="order" value="1" />
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
<property name="supportedMediaTypes" value="application/json"/>
</bean>
<bean id="marshallingHttpMessageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="jaxb2Marshaller" />
<property name="unmarshaller" ref="jaxb2Marshaller" />
<property name="supportedMediaTypes" value="application/xml"/>
</bean>
<bean class = "org.springframework.http.converter.FormHttpMessageConverter">
<property name="supportedMediaTypes" value = "application/x-www-form-urlencoded" />
</bean>
<bean class = "org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
</bean>
</list>
</property>
</bean>
<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller" >
<property name="classesToBeBound">
<list>
<value>com.lt.domain.Book</value>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2" />
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
解决方法:
@RequestMapping (method = RequestMethod.POST)
public Book createBook(Book book)
throws Exception {
return book;
}
consume使用一个字符串数组,它可以消耗任何东西,spring bean绑定应该处理其余的事情.问题可能是你没有正确设置bean绑定,以便自动编组和解组json.使用@RequestBody和@RepsonseBody不是最好的选择imho.
确保将jackson添加到您的依赖项中
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>latest</version>
</dependency>
并使用contentnegotiatingviewresolver
06002
确保在客户端应用程序中将accept头设置为所需的值.
您还应该能够在requestmethod注释中删除所有生成和使用数据
内容总结
以上是互联网集市为您收集整理的java – 处理json和form params的Spring MVC方法全部内容,希望文章能够帮你解决java – 处理json和form params的Spring MVC方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。