一、异常描述
通过springframework的mvc的实现JSON格式数据接口返回,在浏览器中能正常请求到数据,但是通过httpclient调用经过nginx代理访问接口报错“org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation..Could not complete request.”异常,具体错误如下所示
2017-07-10 18:11:51,729 [http-bio-8077-exec-59] ERROR c.c.a.l.a.c.BasicController ? [?] - 异常信息:Could not find acceptable representation@b@2017-07-10 18:11:51,729 [http-bio-8077-exec-59] ERROR c.c.a.l.a.c.BasicController ? [?] - 异常信息:org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:134)@b@org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation@b@at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:134)@b@at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:146)@b@at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:71)@b@at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:126)@b@at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)@b@at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)@b@at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)@b@at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)@b@at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)@b@at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)@b@at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858)@b@at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)@b@at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)@b@at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)@b@at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)@b@at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)@b@at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)@b@at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)@b@at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)@b@at cn.com.autohome.loan.api.filter.CORSFilter.doFilter(CORSFilter.java:24)@b@at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)@b@at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)@b@at cn.com.autohome.loan.api.filter.XssFilter.doFilter(XssFilter.java:27)@b@at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)@b@at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)@b@at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)@b@at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)@b@at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)@b@at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)@b@at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)@b@at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)@b@at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)@b@at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)@b@at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)@b@at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)@b@at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)@b@at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)@b@at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)@b@at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)@b@at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)@b@at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)@b@at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)@b@at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)@b@at java.lang.Thread.run(Thread.java:745)@b@2017-07-10 18:11:51,734 [http-bio-8077-exec-59] DEBUG o.s.w.s.DispatcherServlet ? [?] - Could not complete request@b@org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation@b@at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:134) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]@b@at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:146) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]@b@at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:71) ~[spring-web-4.1.7.RELEASE.jar:4.1.7.RELEASE]@b@at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:126) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]@b@at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]@b@at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]@b@at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]@b@at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]@b@at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]@b@at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967) [spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]@b@at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858) [spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]@b@at javax.servlet.http.HttpServlet.service(HttpServlet.java:624) [servlet-api.jar:na]@b@at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843) [spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]@b@at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) [servlet-api.jar:na]@b@at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) [catalina.jar:7.0.65]@b@at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.65]@b@at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat7-websocket.jar:7.0.65]@b@at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) [catalina.jar:7.0.65]@b@at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.65]@b@at com.xwood.api.filter.GWFilter.doFilter(GWFilter.java:24) [classes/:na]@b@at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) [catalina.jar:7.0.65]@b@at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.65]@b@at com.xwood.api.filter.XssFilter.doFilter(XssFilter.java:27) [classes/:na]@b@at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) [catalina.jar:7.0.65]@b@at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.65]@b@at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85) [spring-web-4.1.7.RELEASE.jar:4.1.7.RELEASE]@b@at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.1.7.RELEASE.jar:4.1.7.RELEASE]@b@at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) [catalina.jar:7.0.65]@b@at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) [catalina.jar:7.0.65]@b@at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) [catalina.jar:7.0.65]@b@at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) [catalina.jar:7.0.65]@b@at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) [catalina.jar:7.0.65]@b@at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) [catalina.jar:7.0.65]@b@at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) [catalina.jar:7.0.65]@b@at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) [catalina.jar:7.0.65]@b@at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) [catalina.jar:7.0.65]@b@at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423) [catalina.jar:7.0.65]@b@at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079) [tomcat-coyote.jar:7.0.65]@b@at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625) [tomcat-coyote.jar:7.0.65]@b@at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316) [tomcat-coyote.jar:7.0.65]@b@at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_65]@b@at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_65]@b@at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-coyote.jar:7.0.65]@b@at java.lang.Thread.run(Thread.java:745) [na:1.7.0_65]
二、解决方法
1. 确认spring配置支持html文本json格式的支持,如下所示
2. 根据报错日志的"com.xwood.api.filter.GWFilter.doFilter(GWFilter.java:24) "在之前打印客户端请求的request.getContentType()内容,如下图所示
3. 从2中可以看出问题是因为配置nginx反向代理造成将默认返回的文本类型统一改为“Content-Type :application/x-www-form-urlencoded; charset=UTF-8 ”,可以服务端接口只接受“application/json”或者“application/html”格式