在Spring3.1以后,若是使用<mvc:annotation-driven />,即便用注解驱动,默认状况下已经配置了MappingJackson2HttpMessageConverter,那么只要加入对应的实现jar包便可:web
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.5</version> </dependency>
若是没有使用<mvc:annotation-driven />,则须要手动配置MessageConverter:spring
<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数据: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; }