转自:https://www.cnblogs.com/ssslinppp/p/4528892.html

  1. 【Spring学习笔记-MVC-3】SpringMVC返回Json数据-方式1:http://www.cnblogs.com/ssslinppp/p/4528892.html
  2. 【Spring学习笔记-MVC-4】返回Json数据-方式2:http://www.cnblogs.com/ssslinppp/p/4530002.html
  3. 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展:http://www.cnblogs.com/ssslinppp/p/4675495.html
文章的内容主要如下:

  1. 方式1:讲解如果返回单个对象的json;==>使用@ResponseBody来实现;注解方式
  2. 方式2:讲解如果返回多个对象的json;==>使用MappingJacksonJsonView来实现;xml配置方式
  3. 方式1-扩展:讲解如果返回多个对象的json;==>使用@ResponseBody来实现;注解方式
个人认为,使用@ResponseBody方式来实现json数据的返回比较方便,推荐使用。

摘要


使用Spring MVC,实现json数据的返回。
主要步骤:
  1. 额外添加2个jar包;
  2. 使用 @ResponseBody声明返回值;
  3. 配置<mvc:annotation-driven />;==>需要引入:xmlns:mvc="http://www.springframework.org/schema/mvc";

@ResponseBody:

作用: 
该注解用于将Controller方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后(如:json格式),写入到Response对象的body数据区。 
使用时机:
返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;


<mvc:annotation-driven /> :

<mvc:annotation-driven /> 会自动注册
  1. DefaultAnnotationHandlerMapping
  2. AnnotationMethodHandlerAdapter
两个bean,这两个bean是spring MVC为@Controllers分发请求所必须的。并提供了数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)。


需要的jar包


上面两个都是必须的。

项目结构


程序代码


Shop.java


  1. package com.ll.model;
  2. public class Shop {
  3. String name;
  4. String staffName[];
  5. public String getName() {
  6. return name;
  7. }
  8. public void setName(String name) {
  9. this.name = name;
  10. }
  11. public String[] getStaffName() {
  12. return staffName;
  13. }
  14. public void setStaffName(String[] staffName) {
  15. this.staffName = staffName;
  16. }
  17. }

JSONController.java

  1. package com.ll.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.PathVariable;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7. import com.ll.model.Shop;
  8. @Controller
  9. @RequestMapping("/kfc/brands")
  10. public class JSONController {
  11. @RequestMapping(value="{name}", method = RequestMethod.GET)
  12. public @ResponseBody Shop getShopInJSON(@PathVariable String name) {
  13. System.out.println("-----请求json数据--------");
  14. Shop shop = new Shop();
  15. shop.setName(name);
  16. shop.setStaffName(new String[]{"mkyong1", "mkyong2"});
  17. return shop;
  18. }
  19. }

添加: @ResponseBody作为返回值。


web.xml

  1. <web-app id="WebApp_ID" version="2.4"
  2. xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  4. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  5. <display-name>Spring Web MVC Application</display-name>
  6. <context-param>
  7. <param-name>contextConfigLocation</param-name>
  8. <param-value>classpath:applicationContext.xml</param-value>
  9. </context-param>
  10. <listener>
  11. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  12. </listener>
  13. <servlet>
  14. <servlet-name>mvc-dispatcher</servlet-name>
  15. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  16. <load-on-startup>1</load-on-startup>
  17. </servlet>
  18. <servlet-mapping>
  19. <servlet-name>mvc-dispatcher</servlet-name>
  20. <url-pattern>/rest/*</url-pattern>
  21. </servlet-mapping>
  22. </web-app>

mvc-dispatcher-servlet.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:context="http://www.springframework.org/schema/context"
  3. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  9. http://www.springframework.org/schema/mvc
  10. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
  11. <context:component-scan base-package="com.ll.controller" />
  12. <mvc:annotation-driven />
  13. </beans>

开启:<mvc:annotation-driven />



applicationContext.xml


  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  10. http://www.springframework.org/schema/tx
  11. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
  14. <!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->
  15. <context:component-scan base-package="com.ll.model"/>
  16. </beans>

运行



参考网站


http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/

http://my.oschina.net/abian/blog/128028  

来自为知笔记(Wiz)

转载于:https://www.cnblogs.com/sharpest/p/5764579.html

1.《Spring学习笔记-MVC》系列文章,讲解返回json数据的文章共有3篇,分别为:...相关推荐

  1. 【Spring学习笔记-MVC-13.2】Spring MVC之多文件上传

    作者:ssslinppp       1. 摘要 前篇文章讲解了单文件上传<[Spring学习笔记-MVC-13]Spring MVC之文件上传>http://www.cnblogs.co ...

  2. Spring学习笔记之MyBatis

    系列文章目录 Spring学习笔记 之 Springhttps://blog.csdn.net/weixin_43985478/article/details/124411746?spm=1001.2 ...

  3. Deep Learning(深度学习)学习笔记整理系列三

    Deep Learning(深度学习)学习笔记整理系列 声明: 1)该Deep Learning的学习系列是整理自网上很大牛和机器学习专家所无私奉献的资料的.具体引用的资料请看参考文献.具体的版本声明 ...

  4. Sharepoint学习笔记—架构系列

     为便于查阅,这里整理并列出了我的Sharepoint学习笔记中涉及架构方面的有关文章,有些内容可能会在以后更新. Sharepoin学习笔记-架构系列--  Sharepoint的网页(Page), ...

  5. Sharepoint学习笔记—Ribbon系列

     为便于查阅,这里整理并列出了我的Sharepoint学习笔记中涉及Ribbon开发的关文章,有些内容可能会在以后更新. Sharepoint学习笔记-Ribbon系列-- 1. Ribbon的架构 ...

  6. spring学习笔记06-spring整合junit(出现的问题,解决的思路)

    spring学习笔记06-spring整合junit(出现的问题,解决的思路) 文章目录 spring学习笔记06-spring整合junit(出现的问题,解决的思路) 3.1测试类中的问题和解决思路 ...

  7. PCA(主成分分析-principal components analysis)学习笔记以及源代码实战讲解

    PCA(主成分分析-principal components analysis)学习笔记以及源代码实战讲解 文章目录 PCA(主成分分析-principal components analysis)学 ...

  8. 【转载】Deep Learning(深度学习)学习笔记整理系列

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0  2013-0 ...

  9. Deep Learning(深度学习)学习笔记整理系列之(三)

    Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...

  10. Deep Learning(深度学习)学习笔记整理系列 | @Get社区

    Deep Learning(深度学习)学习笔记整理系列 | @Get社区 Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net ...

最新文章

  1. JS如何从数组中随机取出若干个数,且不重复
  2. (Java常用类)Object类
  3. 降低软件复杂性一般原则和方法
  4. python教程简书_Python快速教程
  5. 贾跃亭又成功拿到6亿融资!九城与法拉第未来签约...
  6. PostgreSQL 给数据库添加用户
  7. 微服务配置中心实战:Spring + MyBatis + Druid + Nacos 1
  8. 计算机网络——网络层
  9. 为什么安装了python桌面没有图标怎嘛办_安装了软件找不到图标怎么办_电脑软件安装了为什么不见图标...
  10. Linux系统安全强化指南
  11. 计算机科学与技术哪些专业课,计算机科学与技术专业课程有哪些 计算机科学与技术有哪些科目...
  12. 山东理工大学ACM平台题答案关于C语言 1543 Egypt
  13. 好用的待办事项APP有哪些
  14. recovery 升级界面顶部花屏问题分析
  15. Linux中对mariadb数据库的管理
  16. CISCO DHCP配置全程详解
  17. 论文排版——毕业论文给图片下方添加序号(题注)
  18. dede织梦编辑器中插入mp4视频文件方法
  19. #UI教程#3分钟打造金色玫瑰
  20. electerm可用的 Termius Dark主题

热门文章

  1. FTP文件同步(java版)
  2. 一个注册表操作类,很有用!
  3. Linux设备模型初始化流程
  4. 驱动模块的安装与卸载指令
  5. 实现透明防火墙的必备知识-Bridge Filter半景
  6. AndroidOpenCV摄像头预览全屏问题
  7. 会场安排问题(贪心算法) Comparator类排序的学习
  8. scala中sorted,sortWith,sortBy用法详解
  9. chrome导入与导出书签
  10. 格雷码的生成详解(C++)(附格雷码对照表)