使用带有REST Web服务的Jackson使用Spring的内置JSON消耗时遇到了一些困难.如果我定义以下内容:
...
@RequestMapping(value="/stub")
public void doSomething(@RequestBody User user) {
System.err.println("In method");
...
}
Run Code Online (Sandbox Code Playgroud)
......它永远不会达到方法.我有杰克逊在课堂上.但是,当我手动使用Jackson时:
@RequestMapping(value="/stub")
public void doSomething(@RequestBody String user) {
System.err.println("In method");
User newUser = null;
try {
user = URLDecoder.decode(user, "UTF-8");
} catch (UnsupportedEncodingException e1) {...}
try {
newUser = new ObjectMapper().readValue(user, User.class);
} catch (Exception e) {...}
}
Run Code Online (Sandbox Code Playgroud)
......它完美无缺.该User对象被所有的正确的价值观正确创建,所以我知道JSON是正确的.它与解码有关吗?据我所知,从Spring 2.5开始,解码默认开启.也许我错过了其他的东西,也许是配置步骤.我的web.xml情况如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
...虽然我的servlet-context.xml定义:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<import resource="mongo-context.xml"/>
<context:component-scan base-package="com.moonlight42.sampleserver.model" />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="prefixJson" value="false"/>
<property name="supportedMediaTypes" value="application/json"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<util:list id="beanList">
<ref bean="jsonHttpMessageConverter"/>
</util:list>
</property>
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
提前致谢.
我相信如果Jackson在类路径中,标签会自动设置Jackson.无需其他配置.
看看添加正确的消耗并在RequestMapping中生成:
@RequestMapping(method = RequestMethod.POST, value = "/stub", consumes = "application/json", produces = "application/json")
Run Code Online (Sandbox Code Playgroud)
这些annoatations是从Spring MVC 3.1项目中提取的.您可能需要用户header="",然后需要正确的MVC标头.
我发现版本3+在这个领域有一些巨大的改进.
您可能遇到以下两个问题之一:
| 归档时间: |
|
| 查看次数: |
14228 次 |
| 最近记录: |