SpringMVC之json数据交互

  在Spring3.1之后,如果使用<mvc:annotation-driven />,即使用注解驱动,默认情况下已经配置了MappingJackson2HttpMessageConverter,那么只要加入对应的实现jar包即可:

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.5</version>
</dependency>

  如果没有使用<mvc:annotation-driven />,则需要手动配置MessageConverter:

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
</list>
</property>
</bean>

  接下来使用@ResponseBody来返回json数据:

@RequestMapping(value="show", method=RequestMethod.GET)
public @ResponseBody User show(){
User user = new User();
user.setUsername("123");
user.setPassword("123");
user.setBirth(new Date());
return user;
}
上一篇:SpringMVC前后端数据交互总结


下一篇:Ubuntu上pip安装uwsgi失败的原因之一(未联网)