目录
一、拦截器对于参数的验证问题
二、尝试方案之使用拦截器
三、使用spring的注解 @ControllerAdvice
一:拦截器对于参数的验证问题
问题场景:
由于之前的参数验证都是放在参数体里面,没有放到方法参数上或者头部,请求数据参数格式为json所以对于所有的接口,我需要验证这公共部分的正确与否,所以我现在需要对请求到controller之前进行拦截,并进行这部分公共参数的验证,所以本身最先想到了两种方案时使用过滤器,要么使用拦截器,但是做的过程中得到第三种方案。具体见下
二、尝试方案之使用拦截器
2.1先上代码如下:
public class AuthInterceptor extends HandlerInterceptorAdapter {
private static final Logger log = Logger.getLogger(AuthInterceptor.class);
//本来时打算在这里面获取到参数,然后对公共部分进行验证
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
String token = request.getParameter("appId");;
if(StringUtils.isblack(token)){
//使用流写出对象,提示token不能为空;
ResultBean result = new ResultBean("10001","token不能为空")
...
return false;
}
//注释一
/*
StringBuffer sb = new StringBuffer();
InputStreamReader isr = new InputStreamReader(request.getInputStream());
BufferedReader br = new BufferedReader(isr);
String s = "";
while ((s = br.readLine()) != null) {
sb.append(s);
}
System.out.println(sb.toString());
*/
//注释二
/*
if(handler instanceof HandlerMethod){
HandlerMethod handlerMethod = (HandlerMethod)handler;
MethodParameter[] methodParameters = handlerMethod.getMethodParameters();
for(MethodParameter methodParameter : methodParameters){
System.out.println(methodParameter.getParameterName());
}
}
}
*/
xml中配置如下:
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*.cgi"/>
<mvc:mapping path="/test/*.cgi"/>
<bean class="com.mouse.moon.common.auth.AuthInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
上述代码部分,可以看出,我在preHandle(…) 方法中想获取参数,然后
在这里面做统一的验证,成功则继续望下走,失败则直接以流的形式写出
错误对象。在前面的问题中已经说明,参数是以json的格式传入到后台,
并且功能部分都是放在json的数据中,所以这里request获取到的数据为
空数据。
2.2 :看到注释一中的代码,我在这里使用流的形式来读取到请求数据,然后此时我获取到相关的参数,可是当我继续往下走时,请求到达controller却报错,错误的意思大致就是说流没了,因为我们在拦截器里面读取了流,所以在controller中没办法再次读取流了,使得其直接抛出异常。异常如下:
org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public com.mouse.moon.common.bean.ResultBean com.mouse.person.controller.PersonController.getPersonInfo(com.mouse.moon.common.bean.ParameBean) throws com.mouse.moon.common.exception.ExceptionAbstract
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:151)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:125)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:78)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2466)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2455)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
主要是“Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing:”
所以我就在想,那么此时我们既然读到了流,然后再把流写出不就可以了,可是我查阅了下资料,发现拦截器没办法进行这操作,倒是过滤器时可以获取到流后再写回去。
然后我又试验了网上说的使用注释二的代码进行参数获取,结果获取的参数都是空的。
所以至此,使用拦截器来做参数验证这一步基本上宣告失败了,必须得另外处理方式,所以打算使用过滤器,但是再解决的过错中跟朋友交流了下,说到有这么一个注解 @ControllerAdvice结合HttpEntity<String> 来使用,我就转门去看了下,得到了我最后的解决方案,当然,过滤器的查到的资料可以试试
https://my.oschina.net/vernon/blog/363693?fromerr=2jheR52d
三、使用spring的注解 @ControllerAdvice
3.1: @ControllerAdvice说明
该注解时再spring3.2之后增加的,见名大概意思是控制器增强。
@ControllerAdvice注解内部使用@ExceptionHandler、@InitBinder、@ModelAttribute注解的方法应用到所有的 @RequestMapping注解的方法。
3.2:描述
我这里使用了结合的方式使用,@ModelAttribute结合@ExceptionHandler进行结合使用,现在@ModelAttribute中进行参数获取,然后转换为相应的对象,获取后对参数进行验证,但是验证失败后我们要怎么处理了,这里面没办法返回错误码呀,所以如果验证失败就抛出异常,然后再@ExceptionHandler中进行统一处理。验证通过就吧获取的参数返回即可,那么此时在controller中获取参数需要使用@ModelAttribute进行获取。代码如下:
@ControllerAdvice
public class ValidateControllerAdvice {
private static final Logger log = Logger.getLogger(ValidateControllerAdvice.class);
@ModelAttribute
public ParameBean getBobyInfo(HttpEntity<String> httpEntity,HttpServletRequest request,HttpServletResponse response) throws JsonParseException, JsonMappingException, IOException, CommonValidateException {
//获取参数
String data = httpEntity.getBody();
ObjectMapper objectMapper = new ObjectMapper();
ParamBean paramBean = data != null ? objectMapper.readValue(data, ParamBean.class) : null;
if (pb != null)
{
String secretId = paramBean.getSecretId();
String token = paramBean.getToken();
if(StringUtils.isBlank(secretId)){
throw new ValidateException( Enums.ERROR_SECRETID_NOTNULL,
"SECRETID不能为空");
}else if(StringUtils.isBlank(token)){
throw new ValidateException( Enums.ERROR_TOKEN_NOT_NULL,
"token不能为空");
}else if(validate(token)){
throw new ValidateException( Enums.ERROR_TOKEN_INVALID,
"无效的token");
}
}
return paramBean;
}
//捕获ValidateException的异常,统一返回
@ExceptionHandler({ValidateException.class})
@ResponseBody
public ResultBean validateException(ValidateException ex){
ResultBean result = new ResultBean();
result.setResultCode(ex.getResultCode());
result.setResultMsg(ex.getResultMsg());
return result;
}
}
四:隐患
几天后,测试发现了一个问题,就是比如我的返回数据,我想转换时间不反回长整形,而是”yyyy-MM-dd HH:mm:ss”的形式,所以我使用jackson的配置方案如下
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="serializationInclusion" value="NON_NULL"/>
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
</bean>
</property>
</bean>
</property>
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
测试得到的结果是
org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: java.io.PushbackInputStream@3a4f5e0b; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: java.io.PushbackInputStream@3a4f5e0b; line: 1, column: 1]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:224)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:208)
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:197)
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:141)
at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.resolveArgument(HttpEntityMethodProcessor.java:123)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:78)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129)
at org.springframework.web.method.annotation.ModelFactory.invokeModelAttributeMethods(ModelFactory.java:136)
at org.springframework.web.method.annotation.ModelFactory.initModel(ModelFactory.java:109)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:792)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2466)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2455)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: java.io.PushbackInputStream@3a4f5e0b; line: 1, column: 1]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:762)
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:59)
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:12)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3066)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2221)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:221)
... 44 more
2016-12-10 16:47:28 [ http-apr-8080-exec-6:20921 ] - [ DEBUG ] Resolving exception from handler [public com.mouse.common.bean.ResultBean com.mouse.person.controller.PersonController.getPersonInfo(com.mouse.common.bean.ParameBean)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: java.io.PushbackInputStream@3a4f5e0b; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: java.io.PushbackInputStream@3a4f5e0b; line: 1, column: 1]
2016-12-10 16:47:28 [ http-apr-8080-exec-6:20921 ] - [ DEBUG ] Resolving exception from handler [public com.mouse.common.bean.ResultBean com.mouse.person.controller.PersonController.getPersonInfo(com.mouse.common.bean.ParameBean) throws com.mouse.common.exception.ExceptionAbstract]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: java.io.PushbackInputStream@3a4f5e0b; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: java.io.PushbackInputStream@3a4f5e0b; line: 1, column: 1]
2016-12-10 16:47:28 [ http-apr-8080-exec-6:20921 ] - [ DEBUG ] Resolving exception from handler [public com.mouse.common.bean.ResultBean com.mouse.person.controller.PersonController.getPersonInfp(com.mouse.common.bean.ParamBean)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: java.io.PushbackInputStream@3a4f5e0b; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: java.io.PushbackInputStream@3a4f5e0b; line: 1, column: 1]
2016-12-10 16:47:28 [ http-apr-8080-exec-6:20921 ] - [ WARN ] Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: java.io.PushbackInputStream@3a4f5e0b; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: java.io.PushbackInputStream@3a4f5e0b; line: 1, column: 1]
2016-12-10 16:47:28 [ http-apr-8080-exec-6:20921 ] - [ DEBUG ] Null ModelAndView returned to DispatcherServlet with name 'SpringMVC': assuming HandlerAdapter completed request handling
2016-12-10 16:47:28 [ http-apr-8080-exec-6:20922 ] - [ DEBUG ] Successfully completed request
想把所有的时间都转换了,但是发现这个在我接收参数的时候都有异常。今天在某位大神的指导下发现了问题所在,我们在加了转换器后,在发送请求的时候会讲json数据使用MappingJackson2HttpMessageConverter此类进行转换,那么到了HttpEntity时已经是对象了但是我们却还是使用HttpEntity<String>接收
所以一直抱上面的错误,转换异常。所以我们可以尝试如下
ObjectMapper objectMapper = new ObjectMapper();
String json="{\n"+"\t\"appId":"\"123567\",\n"+"\t\"appcode":"\"sdasfa\"+"}";
objectMapper.readValue(json,String.class);
此时就会报错如上。所以接收点需要修改为HttpEntity<ParamEngine>的写法,接收处就不需要在转换类型,getBody()后就是ParamEngine对象。
五:浩学习
__
__ _ ____ __| |__ _____ ___
\ \/ \/ / | \ | \\__ \ / _ \
\ /| | / Y \/ __ \( <_> )
\/\_/ |____/|___| (____ /\____/
\/ \/
走在自己的路上,遇到要遇到的人,经历要经历的事,这才是我们需要面对的。努力,come on!!!
公元2016年12月7号凌晨00点35分