spring mvc 是个灵活的开源框架 深受大家的喜爱,正因为灵活,所以一个简单的返回JSON 数据的功能,有太多五花八门的配置,看得让人目不暇接。
我决定给一个比较简单 而又实用的配置:
1.依赖的JAR包 如图 SPRING MVC 我的是3.1的
2.<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>
<!-- 启动注解驱动的 Spring MVC 功能, 注册请求 url 和注解 POJO 类方法 的映射 -->
<mvc:annotation-driven />
<!-- 启动包扫描功能,以便注册带有 @Controller 、 @Service 、 @repository 、 @Component 等注解的类成为
spring 的 bean ,不在此spring容器中扫描注册 @Controller 、 @Service,因为此时的service还没有事务特性-->
<context:component-scan base-package="com" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="index.htm">indexController</prop>
</props>
</property>
</bean>
<!-- JSON视图 -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="1" />
</bean>
<bean id="jsonView" class="sys.RenderJsonView"></bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp" />
<!--
The index controller.
-->
<bean name="indexController"
class="org.springframework.web.servlet.mvc.ParameterizableViewController"
p:viewName="index" />
</beans>
3. RenderJsonView
package sys;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;
/**
* 自定义的JsonView,返回json数据
*
* @author wubei
*
*/
@SuppressWarnings("unchecked")
public class RenderJsonView extends MappingJacksonJsonView {
protected void renderMergedOutputModel(Map<String, Object> model,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Map<?, ?> result = (Map<?, ?>) super.filterModel(model);
Object obj = null;
if (result.values().iterator().hasNext()) {
obj = result.values().iterator().next();
}
JsonConfig config = new JsonConfig();
config.setJsonPropertyFilter(new PropertyFilter() {
public boolean apply(Object arg0, String arg1, Object arg2) {
if (arg2 instanceof Set) {
return true;
}
return false;
}
});
if (obj instanceof List) {
response.getWriter().println(JSONArray.fromObject(obj, config));
} else {
response.getWriter().println(JSONObject.fromObject(obj, config));
}
}
}
4.test
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
*
* @author Administrator
*/
@Controller
public class Test {
@RequestMapping(value="/test.htm")
public ModelAndView test(){
Map<String,Object> map = new HashMap<String,Object>();
map.put("name","fengxiang");
map.put("age",23);
return new ModelAndView("jsonView").addObject(map);
}
}
ok 了
- 大小: 15.2 KB
分享到:
相关推荐
使spring-mvc支持ajax 包含源代码以及使用教程
Spring Web model-view-controller (MVC)框架是围绕 DispatcherServlet 设计的,并分发请求到处理程序(handler),Spring MVC支持可配置的处理程序映射(handler mapping),视图解析(view resolution)、 区域设置...
Spring MVC 3实例,包含上传下载,还有Spring mvc jsr303表单验证技术,还有一个spring mvc ajax json等 欢迎下载 自己研究,简单易懂 如果有注解不懂,可以看看...
主要介绍了自定义spring mvc的json视图的实现思路解析,本文给大家介绍的非常详细,具有参考借鉴价值,需要的朋友可以参考下
Spring Boot & Spring MVC 异常处理的N种方法 参考文档: Spring Boot 1.5.4.RELEASE Spring framework 4.3.9.RELEASE 默认行为 根据Spring Boot官方文档的说法: For machine clients it will produce a JSON ...
弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,...
Spring Json-View向Spring-MVC添加了JavaScript Object Notation(JSON)支持。 它被深度集成到Spring-MVC模块中,并且可以与各种标准控制器一起使用。
Build lightning-fast web applications and REST APIs using Spring MVC and its asynchronous processing capabilities with the view technologies of your choice Explore simplified but powerful data access ...
28.1.1.Spring MVC自动配置 28.1.2.HttpMessageConverters 28.1.3.自定义JSON序列化程序和反序列化程序 28.1.4.MessageCodesResolver的信息 28.1.5.静态内容 28.1.6.欢迎页面 28.1.7.自定义Favicon ...
Auto-configured Spring REST Docs Tests with Mock MVC Auto-configured Spring REST Docs Tests with REST Assured 43.3.20. User Configuration and Slicing 43.3.21. Using Spock to Test Spring Boot ...
springMVC spring mybatis rest风格架构 根据请求的后缀名 解析成json 或者 xml格式的数据
2018-2018031401:04-ch04-view 第四章《视图技术》学习源码 Beetl 模版引擎、JSON 技术 2018-2018062501:05-ch05-data 第五章《数据库访问》学习源码 Spring JDBC Template、Beetl SQL 2018-2018062601:06-ch...
而在基于Spring3 MVC的架构下,对HTTP+JSON的返回类型也有很好的支持。但是,在开发工作中,对功能的升级是基于既定架构是很常见的情况。本人碰到需要用开发基于Struts2的HTTP+JSON返回类型接口就是基于既定框架结构...
具有开发,部署简单,全面和稳定,支持所有http请求方式,支持多视图,并且可以根据自己的要求自定义ViewResolver返回自己想要的结果,目前服务只支持String,JSONView两次返回。microboot优缺点优点支
driven配置、InitBinder注解、数据的格式化、JSR303数据校验、错误消息的显示及国际化、Ajax返回JSON、使用HttpMessageConverter、国际化_通过超链接切换中英文、文件上传、自定义的拦截器、拦截器的零Xml配置、异常...
当您使用Jackson @JsonView接口并且需要更强大和灵活的时候,该库可能会很有用。 有关信息,请点击以下链接。刊物样品您可以在GitHub 查看所有指数库流程图图安装要使用此库,您需要导入依赖项< dependency> ...
展现层: Spring MVC - MVC Framework Java Server Pages (JSP) - View Generation ExtJS - Javascript UI Widget Framework Spring Security - Security Framework Jackson - JSON Generation / Parser Jersey - ...
SpringMVC | MVC框架 | [http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc](http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc) ...
返回JSON: { "code" : "CODE SNIPPET" , "date" : "THE DATE IT WAS POSTED" , "time" : 0 , // Time left to view the snippet (0 - unlimited) "views" : 0 // Number of views left for the snippet (0 - ...
SpringMVC:是基于Spring的一个子框架(MVC框架),功能强于Spring,这个框架主要是解决咱们Controller这一层的问题。 M:model-模型User V:view-视图jsp C:Controller-控制器servlet jQuery框架:是一个程序员使用...