有关取代mvc:annotation-driven使用自定义配置请看:

http://blog.csdn.net/cml_blog/article/details/45222431

1、在项目开发中,自定义全局返回值拦截是非常实用的,就如在Struts2的拦截器中,可以根据Action的返回值自定义返回信息,如果返回SUCCESS就统一返回一个成功的json对象,如果FAIL就返回全局的定义信息

2、配置xml:

<context:component-scan base-package="com.cml.mvc.*" />

	<!-- 取代mvc:annotation-driven> -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
</bean> <!-- 采用SpringMVC自带的JSON转换工具,支持@ResponseBody注解 -->
<bean
class=" org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<!-- 自定义返回值拦截 -->
<property name="responseBodyAdvice">
<list>
<bean class="com.cml.mvc.base.MyResponseBodyAdvice"></bean>
</list>
</property>
<!-- 自定义返回值校验 -->
<property name="customReturnValueHandlers">
<list>
<bean class="com.cml.mvc.base.MyReturnValues"></bean>
</list>
</property> <!-- 注入全局的propertiesEditor -->
<property name="webBindingInitializer">
<bean class="com.cml.mvc.base.BaseWebBindingInital">
<property name="timeFormatter" value="yyyy-MM-dd HH:mm:ss"></property>
</bean>
</property>
<property name="contentNegotiationManager" ref="contentNegotiationManager"></property>
</bean>
<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<bean id="contentNegotiationManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false" />
<property name="favorParameter" value="true" />
<property name="mediaTypes">
<value>
json=application/json
xml=application/xml
</value>
</property>
</bean> <!-- 取代mvc:annotation-driven> end -->

2、自定义返回值拦截类,只要实现 org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice接口即可

package com.cml.mvc.base;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; import com.cml.mvc.beans.Result; public class MyResponseBodyAdvice implements ResponseBodyAdvice<Result> { private static Log log = LogFactory.getLog(MyResponseBodyAdvice.class); @Override
public boolean supports(MethodParameter returnType,
Class<? extends HttpMessageConverter<?>> converterType) {
log.debug("MyResponseBodyAdvice==>supports:" + converterType);
log.debug("MyResponseBodyAdvice==>supports:" + returnType.getClass());
log.debug("MyResponseBodyAdvice==>supports:"
+ MappingJackson2HttpMessageConverter.class
.isAssignableFrom(converterType));
return MappingJackson2HttpMessageConverter.class
.isAssignableFrom(converterType);
} @Override
public Result beforeBodyWrite(Result body, MethodParameter returnType,
MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType,
ServerHttpRequest request, ServerHttpResponse response) { log.debug("MyResponseBodyAdvice==>beforeBodyWrite:" + returnType + ","
+ body);
body.setB("我是后面设置的");
return body;
} }

3、controller中只要返回自定义的Result对象,就可以拦截到转换信息,controller代码

       @RequestMapping("/times")
@ResponseBody
public Result getTime( Integer id) { log.debug("==========>getTime,id:" + id);
Result result=new Result();
result.setA(1);
return result;
}

action中设置了A的值为1,在页面上返回结果:



成功实现自定义返回信息。

SpringMVC 拦截返回值,并自定义的更多相关文章

  1. SpringMVC Controller 返回值几种类型

    SpringMVC Controller 返回值几种类型 2016年06月21日 19:31:14 为who而生 阅读数:4189 标签: Controller 返回值类型spring mvc 更多 ...

  2. C#提高--------------获取方法返回值的自定义特性(Attribute)

    .NET(C#):获取方法返回值的自定义特性(Attribute) 转载 2013年05月08日 10:54:42 1456 来自:http://www.cnblogs.com/mgen/archiv ...

  3. SpringMVC核心——返回值问题

    一.SpringMVC 使用 ModelAndView 来处理返回值问题. 1.ModelAndView 官方描述: Holder for both Model and View in the web ...

  4. springmvc 之 返回值

    springMVC对于controller处理方法返回值的可选类型 spring mvc 支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, Stri ...

  5. SpringMVC Controller 返回值的可选类型

    spring mvc 支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void. ModelAndView @RequestMap ...

  6. loadrunner-获取返回值和自定义参数(参数运算)

    实例:手机端操作,A新增了一条事件(返回结果:事件id,例如:1), A这时需要获取新增产生的事件id,并作为参数进行传递,才能将这条事件上报给B(返回结果:事件id不变,步骤id等于事件id加1), ...

  7. 转SpringMVC Controller 返回值的可选类型

    spring mvc 支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void. ModelAndView @RequestMap ...

  8. spring mvc 第三天【注解实现springmvc Handler返回值为Object 的配置】

    这里使用的是在前台发起请求Handler,后台伪造数据响应给前台, 解决方案:将之前的viewResolver抹掉,配置对应(request)请求的Handler信息如下 之前Handler返回的都直 ...

  9. SpringMVC拦截器与SpringBoot自定义拦截器

    首先我们先回顾一下传统拦截器的写法: 第一步创建一个类实现HandlerInterceptor接口,重写接口的方法. 第二步在XML中进行如下配置,就可以实现自定义拦截器了 SpringBoot实现自 ...

随机推荐

  1. Spring5:Java Config

    @Configuration @Bean @ComponentScan @ImportResource 使用Java的方式配置spring,完全不使用spring配置文件,交给java来做! 两个注解 ...

  2. 重装anaconda的记录,包含设置jupyter kernel

    anaconda安装记录 官网下载最新版 linux:sh xx.sh 注意不要敲太多回车,容易错过配置bash的部分,还要手动添加 (vim ~/.bashrc 手动添加新bash,卸载时也要删掉此 ...

  3. Pycharm 操作数据库

    view--->Tool Buttons,点击Pycharm右侧的Database 1.连接数据库       2.建立一个表,添加数据   通过以上操作把用户名和密码储存到了数据库中  3.连 ...

  4. 0day学习笔记(3)--修改函数返回地址

    环境: devc++(编译改为32位),windows10 源码(来自书中) #include <stdio.h> #define PASSWORD "1234567" ...

  5. linux--配置开发环境 --Apache篇

    现在我的的linux服务器上一般都是使用:Apache 和  Nginx 这两种配置. 你现在安装好了,启动了,也无法通过你服务器绑定的网址访问你的网站. 这是你可以通过这个命令查看一下你的80端口: ...

  6. 免费 https 申请步骤,你必须知道

    不适用 https 加密的网站,基本上就等于在裸奔. 来,开始开始动手做 我的系统是 CentOS6 第一步:安装Certbot Certbot可以用于管理(申请.更新.配置.撤销和删除等)Let's ...

  7. opencv-2-VS2017与QT显示图像

    opencv-2-VS2017与QT显示图像 opencvqtVSC++ 目的 使用 VS 构建第一个 opencv 程序 使用 QT 构建 第一个 opencv 程序 VS 导入 QT 程序 开始 ...

  8. 【Linux常见命令】route命令

    route - show / manipulate the IP routing table route命令用于显示和操作IP路由表. route命令用来显示并设置Linux内核中的网络路由表,rou ...

  9. Linux分类

    Linux versions:http://www.cnblogs.com/sammyliu/articles/4832157.html1. Maintained by organization- D ...

  10. 再砸4.35亿美元,LG疯狂扩建太阳能电池生产线

    LG在收缩高分辨率电视和其他消费电子产品业务的同时,在太阳能面板业务上却很明显一直在进行扩张.LG公司表示,他们将斥资4.35亿美元在韩国工厂增加超过6条生产线,使其太阳能电池生产量能够在2018年达 ...