这是我在很长一段时间内遇到的最奇怪的错误之一。
我有一个ServerResponse POJO,我使用配置了ContentNegotiatingViewResolver的JSON或XML Marshaller从我的Spring MVC REST应用程序回复。
现在我的标准JSON响应如果出现错误应该看起来像 - >
{response:{result:null,error:{code:“500”,message:“Internal Server Error”}}
和类似的XML。
相反,如果出现Hibernate异常,我会得到以下内容:
{
"response": {
"result": null,
"error": {
"message": "Internal Server Error",
"code": "500"
},
"respTime": "100 ms"
},
"exception": {
"cause": null,
"message": null,
"localizedMessage": null,
"stackTrace": [(45)
{
"fileName": "NativeConstructorAccessorImpl.java",
"lineNumber": -2,
"className": "sun.reflect.NativeConstructorAccessorImpl",
"methodName": "newInstance0",
"nativeMethod": true
},
{
"fileName": "NativeConstructorAccessorImpl.java",
"lineNumber": 39,
"className": "sun.reflect.NativeConstructorAccessorImpl",
"methodName": "newInstance",
"nativeMethod": false
},
{
"fileName": "DelegatingConstructorAccessorImpl.java",
"lineNumber": 27,
"className": "sun.reflect.DelegatingConstructorAccessorImpl",
"methodName": "newInstance",
"nativeMethod": false
},
{
"fileName": "Constructor.java",
"lineNumber": 513,
"className": "java.lang.reflect.Constructor",
"methodName": "newInstance",
"nativeMethod": false
},
{
"fileName": "BeanUtils.java",
"lineNumber": 147,
"className": "org.springframework.beans.BeanUtils",
"methodName": "instantiateClass",
"nativeMethod": false
},
{
"fileName": "BeanUtils.java",
"lineNumber": 104,
"className": "org.springframework.beans.BeanUtils",
"methodName": "instantiateClass",
"nativeMethod": false
},
{
"fileName": "ModelAttributeMethodProcessor.java",
"lineNumber": 125,
"className": "org.springframework.web.method.annotation.support.ModelAttributeMethodProcessor",
"methodName": "createDataBinder",
"nativeMethod": false
},
{
"fileName": "ModelAttributeMethodProcessor.java",
"lineNumber": 92,
"className": "org.springframework.web.method.annotation.support.ModelAttributeMethodProcessor",
"methodName": "resolveArgument",
"nativeMethod": false
},
{
"fileName": "HandlerMethodArgumentResolverComposite.java",
"lineNumber": 65,
"className": "org.springframework.web.method.support.HandlerMethodArgumentResolverComposite",
"methodName": "resolveArgument",
"nativeMethod": false
},
{
"fileName": "InvocableHandlerMethod.java",
"lineNumber": 153,
"className": "org.springframework.web.method.support.InvocableHandlerMethod",
"methodName": "getMethodArgumentValues",
"nativeMethod": false
},
{
"fileName": "InvocableHandlerMethod.java",
"lineNumber": 117,
"className": "org.springframework.web.method.support.InvocableHandlerMethod",
"methodName": "invokeForRequest",
"nativeMethod": false
},
{
"fileName": "ModelFactory.java",
"lineNumber": 118,
"className": "org.springframework.web.method.annotation.ModelFactory",
"methodName": "invokeAttributeMethods",
"nativeMethod": false
},
{
"fileName": "ModelFactory.java",
"lineNumber": 100,
"className": "org.springframework.web.method.annotation.ModelFactory",
"methodName": "initModel",
"nativeMethod": false
},
{
"fileName": "RequestMappingHandlerAdapter.java",
"lineNumber": 500,
"className": "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter",
"methodName": "invokeHandlerMethod",
"nativeMethod": false
},
{
"fileName": "RequestMappingHandlerAdapter.java",
"lineNumber": 465,
"className": "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter",
"methodName": "handleInternal",
"nativeMethod": false
},
{
"fileName": "AbstractHandlerMethodAdapter.java",
"lineNumber": 80,
"className": "org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter",
"methodName": "handle",
"nativeMethod": false
},
{
"fileName": "DispatcherServlet.java",
"lineNumber": 863,
"className": "org.springframework.web.servlet.DispatcherServlet",
"methodName": "doDispatch",
"nativeMethod": false
},
{
"fileName": "BaseDispatcherServlet.java",
"lineNumber": 31,
"className": "com.locationguru.framework.base.BaseDispatcherServlet",
"methodName": "doDispatch",
"nativeMethod": false
},
{
"fileName": "DispatcherServlet.java",
"lineNumber": 792,
"className": "org.springframework.web.servlet.DispatcherServlet",
"methodName": "doService",
"nativeMethod": false
},
{
"fileName": "BaseDispatcherServlet.java",
"lineNumber": 24,
"className": "com.locationguru.framework.base.BaseDispatcherServlet",
"methodName": "doService",
"nativeMethod": false
},
{
"fileName": "FrameworkServlet.java",
"lineNumber": 851,
"className": "org.springframework.web.servlet.FrameworkServlet",
"methodName": "processRequest",
"nativeMethod": false
},
{
"fileName": "FrameworkServlet.java",
"lineNumber": 767,
"className": "org.springframework.web.servlet.FrameworkServlet",
"methodName": "doPost",
"nativeMethod": false
},
{
"fileName": "HttpServlet.java",
"lineNumber": 641,
"className": "javax.servlet.http.HttpServlet",
"methodName": "service",
"nativeMethod": false
},
{
"fileName": "BaseDispatcherServlet.java",
"lineNumber": 38,
"className": "com.locationguru.framework.base.BaseDispatcherServlet",
"methodName": "service",
"nativeMethod": false
},
{
"fileName": "HttpServlet.java",
"lineNumber": 722,
"className": "javax.servlet.http.HttpServlet",
"methodName": "service",
"nativeMethod": false
},
{
"fileName": "ApplicationFilterChain.java",
"lineNumber": 304,
"className": "org.apache.catalina.core.ApplicationFilterChain",
"methodName": "internalDoFilter",
"nativeMethod": false
},
{
"fileName": "ApplicationFilterChain.java",
"lineNumber": 210,
"className": "org.apache.catalina.core.ApplicationFilterChain",
"methodName": "doFilter",
"nativeMethod": false
},
{
"fileName": "OpenSessionInViewFilter.java",
"lineNumber": 198,
"className": "org.springframework.orm.hibernate3.support.OpenSessionInViewFilter",
"methodName": "doFilterInternal",
"nativeMethod": false
},
{
"fileName": "OncePerRequestFilter.java",
"lineNumber": 76,
"className": "org.springframework.web.filter.OncePerRequestFilter",
"methodName": "doFilter",
"nativeMethod": false
},
{
"fileName": "ApplicationFilterChain.java",
"lineNumber": 243,
"className": "org.apache.catalina.core.ApplicationFilterChain",
"methodName": "internalDoFilter",
"nativeMethod": false
},
{
"fileName": "ApplicationFilterChain.java",
"lineNumber": 210,
"className": "org.apache.catalina.core.ApplicationFilterChain",
"methodName": "doFilter",
"nativeMethod": false
},
{
"fileName": "StandardWrapperValve.java",
"lineNumber": 224,
"className": "org.apache.catalina.core.StandardWrapperValve",
"methodName": "invoke",
"nativeMethod": false
},
{
"fileName": "StandardContextValve.java",
"lineNumber": 185,
"className": "org.apache.catalina.core.StandardContextValve",
"methodName": "invoke",
"nativeMethod": false
},
{
"fileName": "AuthenticatorBase.java",
"lineNumber": 472,
"className": "org.apache.catalina.authenticator.AuthenticatorBase",
"methodName": "invoke",
"nativeMethod": false
},
{
"fileName": "StandardHostValve.java",
"lineNumber": 151,
"className": "org.apache.catalina.core.StandardHostValve",
"methodName": "invoke",
"nativeMethod": false
},
{
"fileName": "ErrorReportValve.java",
"lineNumber": 100,
"className": "org.apache.catalina.valves.ErrorReportValve",
"methodName": "invoke",
"nativeMethod": false
},
{
"fileName": "AccessLogValve.java",
"lineNumber": 929,
"className": "org.apache.catalina.valves.AccessLogValve",
"methodName": "invoke",
"nativeMethod": false
},
{
"fileName": "StandardEngineValve.java",
"lineNumber": 118,
"className": "org.apache.catalina.core.StandardEngineValve",
"methodName": "invoke",
"nativeMethod": false
},
{
"fileName": "CoyoteAdapter.java",
"lineNumber": 405,
"className": "org.apache.catalina.connector.CoyoteAdapter",
"methodName": "service",
"nativeMethod": false
},
{
"fileName": "Http11Processor.java",
"lineNumber": 269,
"className": "org.apache.coyote.http11.Http11Processor",
"methodName": "process",
"nativeMethod": false
},
{
"fileName": "AbstractProtocol.java",
"lineNumber": 515,
"className": "org.apache.coyote.AbstractProtocol$AbstractConnectionHandler",
"methodName": "process",
"nativeMethod": false
},
{
"fileName": "JIoEndpoint.java",
"lineNumber": 300,
"className": "org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor",
"methodName": "run",
"nativeMethod": false
},
{
"fileName": "ThreadPoolExecutor.java",
"lineNumber": 886,
"className": "java.util.concurrent.ThreadPoolExecutor$Worker",
"methodName": "runTask",
"nativeMethod": false
},
{
"fileName": "ThreadPoolExecutor.java",
"lineNumber": 908,
"className": "java.util.concurrent.ThreadPoolExecutor$Worker",
"methodName": "run",
"nativeMethod": false
},
{
"fileName": "Thread.java",
"lineNumber": 680,
"className": "java.lang.Thread",
"methodName": "run",
"nativeMethod": false
}
]
}
}
虽然XML仍然正确出现。
任何想法?
答案 0 :(得分:1)
以下配置,而Jackson Json Marshaller bean帮助删除了在我的JSON响应中呈现的不必要密钥。
<!-- XStream XML View -->
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
<property name="autodetectAnnotations" value="true"/>
<property name="annotatedClasses">
<list>
<value>framework.response.object.ServerResponse</value>
</list>
</property>
</bean>
</constructor-arg>
</bean>
</list>
</property>
<property name="defaultContentType" value="application/xml"/>
<property name="favorPathExtension" value="true"/>
</bean>
专注于此 - &gt;
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="modelKey" value="response" />
</bean>