SrpingMVC 返回JSON

springmvc配置文件需加入
  1. <beanclass="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
  2. <beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
  3. <propertyname="messageConverters">
  4. <list>
  5. <refbean="mappingJacksonHttpMessageConverter"/>
  6. </list>
  7. </property>
  8. </bean>
  9. <beanid="mappingJacksonHttpMessageConverter"
  10. class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
  11. <propertyname="supportedMediaTypes">
  12. <list>
  13. <value>text/html;charset=UTF-8</value>
  14. </list>
  15. </property>
  16. </bean>
  17. <context:annotation-config/>

注入springmvc配置文件中不要加入

  1. <!--使用注解解析也就是DefaultAnnotationHandlerMapping-->
  2. <mvc:annotation-driven/>



需要2个Jar包

  1. <dependency>
  2. <groupId>org.codehaus.jackson</groupId>
  3. <artifactId>jackson-core-asl</artifactId>
  4. <version>1.8.4</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.codehaus.jackson</groupId>
  8. <artifactId>jackson-mapper-asl</artifactId>
  9. <version>1.8.4</version>
  10. </dependency>

使用如下:

  1. @RequestMapping(value="/create!save.do")
  2. @ResponseBody
  3. publicObjectcreate(@ModelAttributeUseruser)throwsException{
  4. userService.insert(user);
  5. returnuser;
  6. }

你可能感兴趣的:(返回JSON)