ssslinppp

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  274 随笔 :: 1 文章 :: 48 评论 :: 101万 阅读
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

《Spring学习笔记-MVC》系列文章,讲解返回json数据的文章共有3篇,分别为:
  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
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.xml

<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>



mvc-dispatcher-servlet.xml

<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/ 










附件列表

     

    posted on   ssslinppp  阅读(128084)  评论(1编辑  收藏  举报
    编辑推荐:
    · 用 C# 插值字符串处理器写一个 sscanf
    · Java 中堆内存和栈内存上的数据分布和特点
    · 开发中对象命名的一点思考
    · .NET Core内存结构体系(Windows环境)底层原理浅谈
    · C# 深度学习:对抗生成网络(GAN)训练头像生成模型
    阅读排行:
    · 趁着过年的时候手搓了一个低代码框架
    · 本地部署DeepSeek后,没有好看的交互界面怎么行!
    · 为什么说在企业级应用开发中,后端往往是效率杀手?
    · 用 C# 插值字符串处理器写一个 sscanf
    · 乌龟冬眠箱湿度监控系统和AI辅助建议功能的实现
    点击右上角即可分享
    微信分享提示