Marshaller在我的响应对象中添加额外的参数

Marshaller在我的响应对象中添加额外的参数

本文介绍了Spring JSON Jackson Marshaller在我的响应对象中添加额外的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我长期以来遇到的最奇怪的错误之一。

我有一个ServerResponse POJO,我使用JSON或配置了ContentNegotiatingViewResolver的XML Marshaller从我的Spring MVC REST应用程序响应。



现在我的标准JSON响应出现错误应该看起来像 - >



{response:{result: null,error:{code:500,message:Internal Server Error}}



和一个类似的XML。

相反,如果发生Hibernate异常,我会得到以下结果:

  {
响应:{
result:null,
error:{
message:内部服务器错误,
code:500
$ brespTime: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,
m ethodName: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.me thod.annotation.support.ModelAttributeMethodProcessor,
methodName:createDataBinder,
nativeMethod:false
},
{
fileName: ModelAttributeMethodProcessor.java,
lineNumber:92,
className:org.springframework.web.method.annotation.support.ModelAttributeMethodProcessor,$ b $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.Reque stMappingHandlerAdapter,
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
},
{
fil eName:BaseDispatcherServlet.java,
lineNumber:24,
className:com.locationguru.framework.base.BaseDispatcherServlet,$ b $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:服务,
na tiveMethod: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.springframe work.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仍然是正确的。



任何想法?

解决方案

以下配置,而Jackson Json Marshaller bean帮助删除在我的JSON响应中呈现的无用键。

















  <! -  XStream XML视图 - > 
< bean class =org.springframework.web.servlet.view.xml.MarshallingView>
< constructor-arg>
< bean id =xstreamMarshallerclass =org.springframework.oxm.xstream.XStreamMarshaller>
< property name =autodetectAnnotationsvalue =true/>
< property name =annotatedClasses>
< list>
< value> framework.response.object.ServerResponse< / value>
< / list>
< / property>
< / bean>
< / constructor-arg>
< / bean>
< / list>
< / property>
< property name =defaultContentTypevalue =application / xml/>
< property name =favorPathExtensionvalue =true/>
< / bean>

重点在此 - >

 < bean class =org.springframework.web.servlet.view.json.MappingJacksonJsonView> 
< property name =modelKeyvalue =response/>
< / bean>


This is one of the strangest errors I have encountered in a long time.

I have a ServerResponse POJO that I respond with from my Spring MVC REST app using JSON or XML Marshaller configured with ContentNegotiatingViewResolver.

Now my standard JSON response in case of error should look like ->

{ response: { result: null, error: {code: "500", message: "Internal Server Error"}}

and a similar XML.

Instead I'm getting the following in case a Hibernate exception occurs in:

   {
"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
}
]
}
}

While the XML is still coming out correctly.

Any Ideas?

解决方案

The following configuration while Jackson Json Marshaller bean helped remove the unncessary keys getting rendered in my JSON response.

            <!-- 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>

Focus Here -->

<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
    <property name="modelKey" value="response" />
</bean>

这篇关于Spring JSON Jackson Marshaller在我的响应对象中添加额外的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:25