1.《Spring学习笔记-MVC》系列文章,讲解返回json数据的文章共有3篇,分别为:...
转自:https://www.cnblogs.com/ssslinppp/p/4528892.html
- 【Spring学习笔记-MVC-3】SpringMVC返回Json数据-方式1:http://www.cnblogs.com/ssslinppp/p/4528892.html
- 【Spring学习笔记-MVC-4】返回Json数据-方式2:http://www.cnblogs.com/ssslinppp/p/4530002.html
- 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展:http://www.cnblogs.com/ssslinppp/p/4675495.html
- 方式1:讲解如果返回单个对象的json;==>使用@ResponseBody来实现;注解方式
- 方式2:讲解如果返回多个对象的json;==>使用MappingJacksonJsonView来实现;xml配置方式
- 方式1-扩展:讲解如果返回多个对象的json;==>使用@ResponseBody来实现;注解方式
摘要
- 额外添加2个jar包;
- 使用 @ResponseBody声明返回值;
- 配置<mvc:annotation-driven />;==>需要引入:xmlns:mvc="http://www.springframework.org/schema/mvc";
@ResponseBody:
<mvc:annotation-driven /> :
- DefaultAnnotationHandlerMapping
- AnnotationMethodHandlerAdapter
需要的jar包
项目结构
程序代码
Shop.java
package com.ll.model;
public class Shop {
String name;
String staffName[];
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getStaffName() {
return staffName;
}
public void setStaffName(String[] staffName) {
this.staffName = staffName;
}
}
JSONController.java
package com.ll.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ll.model.Shop;
@Controller
@RequestMapping("/kfc/brands")
public class JSONController {
@RequestMapping(value="{name}", method = RequestMethod.GET)
public @ResponseBody Shop getShopInJSON(@PathVariable String name) {
System.out.println("-----请求json数据--------");
Shop shop = new Shop();
shop.setName(name);
shop.setStaffName(new String[]{"mkyong1", "mkyong2"});
return shop;
}
}
添加: @ResponseBody作为返回值。
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring Web MVC Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.ll.controller" />
<mvc:annotation-driven />
</beans>
开启:<mvc:annotation-driven />
applicationContext.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->
<context:component-scan base-package="com.ll.model"/>
</beans>
运行
参考网站
http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/
转载于:https://www.cnblogs.com/sharpest/p/5764579.html
1.《Spring学习笔记-MVC》系列文章,讲解返回json数据的文章共有3篇,分别为:...相关推荐
- 【Spring学习笔记-MVC-13.2】Spring MVC之多文件上传
作者:ssslinppp 1. 摘要 前篇文章讲解了单文件上传<[Spring学习笔记-MVC-13]Spring MVC之文件上传>http://www.cnblogs.co ...
- Spring学习笔记之MyBatis
系列文章目录 Spring学习笔记 之 Springhttps://blog.csdn.net/weixin_43985478/article/details/124411746?spm=1001.2 ...
- Deep Learning(深度学习)学习笔记整理系列三
Deep Learning(深度学习)学习笔记整理系列 声明: 1)该Deep Learning的学习系列是整理自网上很大牛和机器学习专家所无私奉献的资料的.具体引用的资料请看参考文献.具体的版本声明 ...
- Sharepoint学习笔记—架构系列
为便于查阅,这里整理并列出了我的Sharepoint学习笔记中涉及架构方面的有关文章,有些内容可能会在以后更新. Sharepoin学习笔记-架构系列-- Sharepoint的网页(Page), ...
- Sharepoint学习笔记—Ribbon系列
为便于查阅,这里整理并列出了我的Sharepoint学习笔记中涉及Ribbon开发的关文章,有些内容可能会在以后更新. Sharepoint学习笔记-Ribbon系列-- 1. Ribbon的架构 ...
- spring学习笔记06-spring整合junit(出现的问题,解决的思路)
spring学习笔记06-spring整合junit(出现的问题,解决的思路) 文章目录 spring学习笔记06-spring整合junit(出现的问题,解决的思路) 3.1测试类中的问题和解决思路 ...
- PCA(主成分分析-principal components analysis)学习笔记以及源代码实战讲解
PCA(主成分分析-principal components analysis)学习笔记以及源代码实战讲解 文章目录 PCA(主成分分析-principal components analysis)学 ...
- 【转载】Deep Learning(深度学习)学习笔记整理系列
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-0 ...
- Deep Learning(深度学习)学习笔记整理系列之(三)
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
- Deep Learning(深度学习)学习笔记整理系列 | @Get社区
Deep Learning(深度学习)学习笔记整理系列 | @Get社区 Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net ...
最新文章
- JS如何从数组中随机取出若干个数,且不重复
- (Java常用类)Object类
- 降低软件复杂性一般原则和方法
- python教程简书_Python快速教程
- 贾跃亭又成功拿到6亿融资!九城与法拉第未来签约...
- PostgreSQL 给数据库添加用户
- 微服务配置中心实战:Spring + MyBatis + Druid + Nacos 1
- 计算机网络——网络层
- 为什么安装了python桌面没有图标怎嘛办_安装了软件找不到图标怎么办_电脑软件安装了为什么不见图标...
- Linux系统安全强化指南
- 计算机科学与技术哪些专业课,计算机科学与技术专业课程有哪些 计算机科学与技术有哪些科目...
- 山东理工大学ACM平台题答案关于C语言 1543 Egypt
- 好用的待办事项APP有哪些
- recovery 升级界面顶部花屏问题分析
- Linux中对mariadb数据库的管理
- CISCO DHCP配置全程详解
- 论文排版——毕业论文给图片下方添加序号(题注)
- dede织梦编辑器中插入mp4视频文件方法
- #UI教程#3分钟打造金色玫瑰
- electerm可用的 Termius Dark主题