spring mvc:输出xml/输出json

用到的注解@ResponseBody

@ResponseBody用来输出json/xml等格式数据(非html)

controller输出用到的类

org.springframework.web.bind.annotation.ResponseBody

需要bean解析支持

<!-- bean视图解析 -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>

  

用到的json插件

<!-- json插件 -->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.7.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.4</version>
</dependency>

  

项目访问地址:http://localhost:8080/gugua3/person/list

包名:json

配置文件:web.xml, applicationContext.xml, json-servlet.xml

web.xml

<web-app>
<display-name>Archetype Created Web Application</display-name> <!--配置文件路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param> <!-- 字符过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 监听转发 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>json</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>json</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>

  

applicationContext.xml

<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 默认:注解映射支持 -->
<mvc:annotation-driven/>
<!-- 静态资源配置 -->
<mvc:resources location="/pages/**" mapping="/pages/"/> <!-- 自动扫描包名,controller -->
<context:component-scan base-package="json"/> </beans>

  

json-servlet.xml

<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean视图解析 -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/> </beans>

  

Person.java

package json;

public class Person {

	String name;
String passwd; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
} }

  

PersonController.java

package json;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@RequestMapping(value="/person")
public class PersonController { @RequestMapping(value="/list", method=RequestMethod.GET)
@ResponseBody
public Map<String, Object> getPerson()
{
List<Person> list = new ArrayList<Person>();
Person person1 = new Person();
person1.setName("张三");
person1.setPasswd("123456");
list.add(person1); Person person2 = new Person();
person2.setName("李四");
person2.setPasswd("456789");
list.add(person2); Map<String, Object> maps = new HashMap<String, Object>(3);
maps.put("work", "研发");
maps.put("school", "suzhou");
maps.put("list", list);
return maps; } }

  

spring mvc:输出json,输出多个json的更多相关文章

  1. Spring MVC Controller与jquery ajax请求处理json

    在用 spring mvc 写应用的时候发现jquery传递的[json数组对象]参数后台接收不到,多订单的处理,ajax请求: "}]}]} $.ajax({ url : url, typ ...

  2. spring MVC 如何接收前台传入的JSON对象数组并处理

    spring MVC 如何接收前台传入的JSON对象数组 主要方法: (主要用到的包是 net.sf.json  即:json-lib-2.3-jdk15.jar 完整相关jar包: commons- ...

  3. spring MVC 如何接收前台传入的JSON对象数组

    spring MVC 如何接收前台传入的JSON对象数组 主要方法: (主要用到的包是 net.sf.json  即:json-lib-2.3-jdk15.jar 完整相关jar包: commons- ...

  4. Spring MVC 学习笔记11 —— 后端返回json格式数据

    Spring MVC 学习笔记11 -- 后端返回json格式数据 我们常常听说json数据,首先,什么是json数据,总结起来,有以下几点: 1. JSON的全称是"JavaScript ...

  5. Spring MVC 后端获取前端提交的json格式字符串并直接转换成control方法对应的参数对象

    场景: 在web应用开发中,spring mvc凭借出现的性能和良好的可扩展性,导致使用日渐增多,成为事实标准,在日常的开发过程中,有一个很常见的场景:即前端通过ajax提交方式,提交参数为一个jso ...

  6. spring mvc ajaxfileupload文件上传返回json下载问题

    问题:使用spring mvc ajaxfileupload 文件上传在ie8下会提示json下载问题 解决方案如下: 服务器代码: @RequestMapping(value = "/ad ...

  7. Spring MVC控制器用@ResponseBody声明返回json数据报406的问题

    本打算今天早点下班,结果下午测试调试程序发现一个问题纠结到晚上才解决,现在写一篇博客来总结下. 是这样的,本人在Spring mvc控制层用到了@ResponseBody标注,以便返回的数据为json ...

  8. 转转转!!Spring MVC控制器用@ResponseBody声明返回json数据报406的问题

    本打算今天早点下班,结果下午测试调试程序发现一个问题纠结到晚上才解决,现在写一篇博客来总结下. 是这样的,本人在Spring mvc控制层用到了@ResponseBody标注,以便返回的数据为json ...

  9. Spring MVC接收参数(Map,List,JSON,Date,2个Bean)(记录一次面试惨状)

    题目Spring MVC 接收参数 MapListDate2个BeanJSON Spring MVC接收参数 -Map Spring MVC接收参数 -List Spring MVC接收参数 -dat ...

  10. Spring MVC 中 AJAX请求并返回JSON

    一.以ModelAndView的方式返回 先看下JavaScript代码: /** * 保存-同步(版本控制库) */ function saveSynchronizedVcHorse(obj) { ...

随机推荐

  1. 003-shell 传递参数

    一.概述 可以在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:$n.n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推…… 二.实例 以下实例我们向 ...

  2. linux ipc/its

    linux进程间双向消息队列 server.c #include <stdio.h> #include <stdlib.h> #include <string.h> ...

  3. 使用QJM构建HDFS HA架构(2.2+)

    转载自:http://blog.csdn.net/a822631129/article/details/51313145 本文主要介绍HDFS HA特性,以及如何使用QJM(Quorum Journa ...

  4. python3 爬虫之Pyquery的使用方法

    安装 pip install pyquery 官方文档: https://pythonhosted.org/pyquery/ 初始化方式(四种) 1. 直接字符串 from pyquery impor ...

  5. EventFiringWebDriver网页事件监听(二)

    public class SeleniumDemo { /** * @param args */ public static void main(String[] args) { WebDriver ...

  6. 3.MySQL必知必会之检索数据-SELECT语句

    本章将介绍如何使用SELECT语句从表中检索一个或多个数据列. 1.SELECT语句 SQL语句是由简单的英语单词构成的.这些单词称为关键字,每个SQL语句都是由一个或多个关键字构成的.大概,最经常使 ...

  7. 0728am thinkphp介绍

  8. Mybatis中的#与$的区别

    一.对比场景 场景:数据库分表时,需要将分表的表序号传入的sql中. SpringBoot中使用注解如下: @Insert("insert into collect_#{tblNum}(id ...

  9. JQuery归纳总结(增加中...)

    一.this与$(this)的区别 this指向标签本身对象,而$(this)会将其封装成JQuery对象 如: $(" img").mousemove( function(e){ ...

  10. ss+proxifier灵活控制网络代理

    SS相比大家都知道,不多说. proxifier可能知道的不是很多(至少在今天之前我是不知道的...可能我孤陋寡闻吧) 之前用ss基本上就是chrome SwitchyOmega+SS实现chrome ...