SpringMVC拦截返回值,并⾃定义
有关取代mvc:annotation-driven使⽤⾃定义配置请看:
1、在项⽬开发中,⾃定义全局返回值拦截是⾮常实⽤的,就如在Struts2的中,可以根据Action的返回值⾃定义返回信息,如果返回SUCCESS就统⼀返回⼀个成功的json对象,如果FAIL就返回全局的定义信息
2、配置xml:
<context:component-scan base-package="l.mvc.*" />
<!-- 取代mvc:annotation-driven> -->
<bean
class="org.springframework.web.hod.annotation.RequestMappingHandlerMapping">
</bean>
<!-- 采⽤SpringMVC⾃带的JSON转换⼯具,⽀持@ResponseBody注解 -->
<bean
class=" org.springframework.web.hod.annotation.RequestMappingHandlerAdapter">
<!-- ⾃定义返回值拦截 -->
<property name="responseBodyAdvice">
<list>
<bean class="l.mvc.base.MyResponseBodyAdvice"></bean>
</list>
</property>
<!-- ⾃定义返回值校验 -->
<property name="customReturnValueHandlers">
<list>
<bean class="l.mvc.base.MyReturnValues"></bean>
</list>
</property>
<!-- 注⼊全局的propertiesEditor -->
<property name="webBindingInitializer">
<bean class="l.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.verter.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.hod.annotation.ResponseBodyAdvice接⼝即可
l.mvc.base;
import org.apachemons.logging.Log;
import org.apachemons.logging.LogFactory;
import MethodParameter;
import org.springframework.http.MediaType;
import org.verter.HttpMessageConverter;
import org.verter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.hod.annotation.ResponseBodyAdvice;
l.mvc.beans.Result;
public class MyResponseBodyAdvice implements ResponseBodyAdvice<Result> {
private static Log log = Log(MyResponseBodyAdvice.class);
@Override
public boolean supports(MethodParameter returnType,
Class<? extends HttpMessageConverter<?>> converterType) {
write的返回值log.debug("MyResponseBodyAdvice==>supports:" + converterType);
log.debug("MyResponseBodyAdvice==>supports:" + Class());
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,在页⾯上返回结果:
成功实现⾃定义返回信息。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论