在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; }