SpringMVC已经大行其道。一般的,都是返回JSP视图。如果需要返回JSON格式,我们大都掌握了一些方法。

  在ContentNegotiatingViewResolver之前,一般使用XmlViewResolver的location属性,手工编写一个视图专门处理Json类型,就是将返回的数据封装成Json输出。下面介绍该方案具体代码。

  exam-servlet.xml:

    <bean id="xmlViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="order" value="1"/>
<property name="location" value="/WEB-INF/views.xml"/>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="2"></property>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
</bean>

  views.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="json" class="cc.monggo.web.views.JsonView"></bean>
</beans>

  JsonView.java

/**
*
*/
package cc.monggo.web.views; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.filters.OrPropertyFilter;
import net.sf.json.util.PropertyFilter; import org.springframework.web.servlet.view.AbstractView; import cc.monggo.web.form.BaseForm; /**
* @author fangjinsong
*
*/
public class JsonView extends AbstractView {
private static final String DEFAULT_JSON_CONTENT_TYPE = "application/json;charset=UTF-8";
private boolean forceTopLevelArray = false;
private boolean skipBindingResult = true;
private JsonConfig jsonConfig = new JsonConfig(); public JsonView() {
super();
setContentType(DEFAULT_JSON_CONTENT_TYPE);
} public boolean isForceTopLevelArray() {
return forceTopLevelArray;
} public void setForceTopLevelArray(boolean forceTopLevelArray) {
this.forceTopLevelArray = forceTopLevelArray;
} public boolean isSkipBindingResult() {
return skipBindingResult;
} public void setSkipBindingResult(boolean skipBindingResult) {
this.skipBindingResult = skipBindingResult;
} public JsonConfig getJsonConfig() {
return jsonConfig;
} public void setJsonConfig(JsonConfig jsonConfig) {
this.jsonConfig = jsonConfig != null ? jsonConfig : new JsonConfig();
if (skipBindingResult) {
PropertyFilter jsonPropertyFilter = this.jsonConfig.getJavaPropertyFilter();
if (jsonPropertyFilter == null) {
this.jsonConfig.setJsonPropertyFilter(new BindingResultPropertyFilter());
} else {
this.jsonConfig.setJsonPropertyFilter(new OrPropertyFilter(new BindingResultPropertyFilter(), jsonPropertyFilter));
}
}
} public static String getDefaultJsonContentType() {
return DEFAULT_JSON_CONTENT_TYPE;
} public boolean isIgnoreDefaultExcludes() {
return getJsonConfig().isIgnoreDefaultExcludes();
} public void setExcludedProperties(String[] excludedProperties) {
jsonConfig.setExcludes(excludedProperties);
} public void setIgnoreDefaultExcludes(boolean ignoreDefaultExcludes) {
jsonConfig.setIgnoreDefaultExcludes(ignoreDefaultExcludes);
} protected String[] getExcludedProperties() {
return jsonConfig.getExcludes();
} @Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType(getContentType());
Map newModel = new HashMap();
for (Iterator it = model.keySet().iterator(); it.hasNext();) {
Object key = it.next();
Object value = model.get(key);
if (!(value instanceof BaseForm)) {
newModel.put(key, value);
}
}
writeJSON(newModel, request, response);
} protected void writeJSON(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
JSON json = createJSON(model, request, response);
if (forceTopLevelArray) {
json = new JSONArray().element(json);
}
json.write(response.getWriter());
} protected final JSON defaultCreateJSON(Map model) {
if (skipBindingResult && jsonConfig.getJavaPropertyFilter() == null) {
jsonConfig.setJsonPropertyFilter(new BindingResultPropertyFilter());
}
return JSONSerializer.toJSON(model, jsonConfig);
} private JSON createJSON(Map model, HttpServletRequest request, HttpServletResponse response) {
return defaultCreateJSON(model);
} private static class BindingResultPropertyFilter implements PropertyFilter {
@Override
public boolean apply(Object source, String name, Object value) {
return name.startsWith("org.springframework.validation.BindingResult");
}
} }

  这种方案会增加一个views.xml文件。另一个方案使用ContentNegotiatingViewResolver内容协商解析器。该解析器可以解析多种Controller返回结果,方法是分派给其他的解析器解析。这样就可以返回JSP视图,Xml视图,Json视图。其中Json视图由MappingJacksonJsonView视图表示。ContentNegotiatingViewResolver的一般配置如下:

    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="order" value="1" />
<property name="ignoreAcceptHeader" value="true" />
<property name="favorParameter" value="false" />
<property name="defaultContentType" value="text/html" />
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
</map>
</property>
<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"></property>
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean id="json" class="com.mogee.web.views.MogeeMappingJacksonJsonView" />
</list>
</property>
</bean>

  要返回Json格式数据,Controller中一般使用JsonResult,@responseBody等技术。例如:

@RequestMapping("/json3.json")
public JsonResult testJson3(@RequestBody User u){
log.info("handle json output from ContentNegotiatingViewResolver");
return new JsonResult(true,"return ok");
}

  这样一来,有一个问题:Controller总是返回Object对象,不能返回ModelAndview类型。按笔者的经验,经常有这样的需求:一个请求,如果返回了正确的结果,返回正常的JSP;如果返回了错误的结果,我们希望返回Json类型数据。为了满足这样要求,一般使用response.getWriter.print("")技术。笔者并不喜欢这样写法,不像Spring的风格。所以下面笔者将提出第二种方案。两者都能兼顾。Contoller代码如下。

@RequestMapping(value = "/test", method = RequestMethod.GET)
public ModelAndView jsonView(HttpServletRequest request, HttpServletResponse response, HelloForm form)
throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("hello", "Hello");
ModelAndView mv = new ModelAndView(new MappingJacksonJsonView(), map);
return mv;
}

  通过这样的代码,可以在Controller中灵活控制,返回JSON或者JSP都可以。看似完美的方案,也有一个问题,返回的时候会将Form也打包到Json中去。如下:

  原因是因为MappingJacksonJsonView代码中将数据map,mv.add(),还有form,还有错误提示等四项数据都加入Json,原程序只是过滤了错误提示,所以就留下了form数据。

为此我们需要重写MappingJacksonJsonView,将剩余三种数据中的form也过滤掉。一般的,form都会继承BaseForm,所以只要使用instanceof判断即可。这样的改造简洁明了,符合Spring编程的特点。具体代码如下:

public class MogeeMappingJacksonJsonView extends MappingJacksonJsonView {
@Override
protected Object filterModel(Map<String, Object> model) {
Map<String, Object> result = new HashMap<String, Object>(model.size());
Set<String> renderedAttributes = !CollectionUtils.isEmpty(super.getRenderedAttributes()) ? super
.getRenderedAttributes() : model.keySet();
for (Map.Entry<String, Object> entry : model.entrySet()) {
if (!(entry.getValue() instanceof BindingResult) && renderedAttributes.contains(entry.getKey())) {
if (!(entry.getValue() instanceof BaseForm)) {
result.put(entry.getKey(), entry.getValue());
}
}
}
return result;
}
}

相应的,exam-servlet.xml中也要修改,代码如下:

如此,就完成了输出Json的方案。

                                                      方劲松 东莞虎门信丰物流

                                                        2015.3.20

SpringMVC返回JSON方案的更多相关文章

  1. 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

  2. 【Spring学习笔记-MVC-4】SpringMVC返回Json数据-方式2

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

  3. 【Spring学习笔记-MVC-3】SpringMVC返回Json数据-方式1

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

  4. SpringMVC返回Json,自定义Json中Date类型格式

    http://www.cnblogs.com/jsczljh/p/3654636.html —————————————————————————————————————————————————————— ...

  5. 配置SpringMVC返回JSON遇到的坑

    坑一:官方网站下载地址不明朗,最后找了几个下载地址:http://wiki.fasterxml.com/JacksonDownload Jackson2.5下载地址:jackson2.5.0.jar ...

  6. SpringMVC返回JSON数据时日期格式化问题

    https://dannywei.iteye.com/blog/2022929 SpringMVC返回JSON数据时日期格式化问题 博客分类: Spring   在运用SpringMVC框架开发时,可 ...

  7. springmvc 返回json数据给前台jsp页面展示

    spring mvc返回json字符串的方式 方案一:使用@ResponseBody 注解返回响应体 直接将返回值序列化json            优点:不需要自己再处理 步骤一:在spring- ...

  8. spring入门(七)【springMVC返回json串】

    现在多数的应用为了提高交互性多使用异步刷新,即在不刷新整个页面的情况下,只刷新局部,局部刷新用得最多就是ajax,ajax和后台进行交互的数据格式使用的最多的是JSON,这里简单描述,在springm ...

  9. springmvc返回json字符串中文乱码问题

    问题: 后台代码如下: @RequestMapping("menuTreeAjax") @ResponseBody /** * 根据parentMenuId获取菜单的树结构 * @ ...

随机推荐

  1. 微服务RPC框架选美

    原文:http://p.primeton.com/articles/59030eeda6f2a40690f03629 1.RPC 框架谁最美? Hello,everybody!说到RPC框架,可能大家 ...

  2. 20155313 杨瀚 《网络对抗技术》实验五 MSF基础应用

    20155313 杨瀚 <网络对抗技术>实验五 MSF基础应用 一.实验目的 本实验目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: 1.一个主动 ...

  3. 20155318 《网络攻防》Exp2 后门原理与实践

    20155318 <网络攻防>Exp2 后门原理与实践 基础问题回答 例举你能想到的一个后门进入到你系统中的可能方式? 下载软件前要勾选一些用户协议,其中部分就存在后门进入系统的安全隐患. ...

  4. Scala学习(一)练习

    Scala基础学习&l练习 1. 在Scala REPL中键人3.,然后按Tab键.有哪些方法可以被应用 在Scala REPL中需要按3. 然后按Tab才会提示. 直接按3加Tab是没有提示 ...

  5. Hadoop日记Day4---去除HADOOP_HOME is deprecated

    去除hadoop运行时的警告 1. 档hadoop运行时,我们会看到如下图1.1所示的警告. 图 1.1 2. 虽然不影响程序运行,但是看到这样的警告信息总是觉得自己做得不够好.一步步分析,先看一下启 ...

  6. vector 去重

    1.使用unique函数: sort(v.begin(),v.end()); v.erase(unique(v.begin(), v.end()), v.end()); //unique()函数将重复 ...

  7. 继承类中static数据值

    class A{ static int num = 1; public static void Display(){ System.out.println( num ); } } class B ex ...

  8. vue 监听页面宽度变化 和 键盘事件

    vue 监听页面窗口大小 export default { name: 'Full', components: { Header, Siderbar }, data () { return { scr ...

  9. 6、Docker图形化管理(Portainer)

    一.Portainer简介 Portainer是Docker的图形化管理工具,提供状态显示面板.应用模板快速部署.容器镜像网络数据卷的基本操作(包括上传下载镜像,创建容器等操作).事件日志显示.容器控 ...

  10. CSS快速入门-前端布局2(唯品会1)

    上一篇我模仿了抽屉网站,这一节我来对唯品会主页进行模仿. 我觉得写一个主页大概思路如下: 1.确定整体布局方式:(html框架布局) 2.针对每一块进行细化,尽量做到模块化.(css) 3.加上特效效 ...