How to remove namespace prefixes from JSON response in CXF/JAX-RS?

我的未来我决定 提交于 2019-12-10 13:47:03

问题


I have the following response created by CXF using JAX-RS:

{"ns1.CustomerInformationResponse":{
  "@xsi.type":"ns1:CustomerInformationResponse",
  "ns2.code":"SUCCESS",
  "ns1.customer":{
    "@xsi.type":"ns2:CustomerBaseDTO",
    "ns2.login":"login1"
  }
}}

And here is the relevant portion of my current context configuration:

<jaxrs:server address="http://${host}:${port}/rest/customer">
  <jaxrs:serviceBeans>
    <ref bean="customerManagementServiceImpl" />
  </jaxrs:serviceBeans>
  <jaxrs:providers>
    <bean class="org.apache.cxf.jaxrs.provider.AegisJSONProvider" />
  </jaxrs:providers>
</jaxrs:server>

I would like to receive responses like the one above, without the namespace prefixes (i.e. ns1, ns2, etc...).

I have tried to add org.apache.cxf.interceptor.transform.TransformOutInterceptor to cxf:outInterceptors, but it did not help.


回答1:


In your beans.xml, you can specife the bean jsonProvider like below.

<bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.AegisJSONProvider">
<property name="ignoreNamespaces" value="true" />
</bean>

Now Invoke the jsonProvider in the Context Configuration as below

<jaxrs:server address="http://${host}:${port}/rest/customer">
    <jaxrs:serviceBeans>
        <ref bean="customerManagementServiceImpl" />
    </jaxrs:serviceBeans>
    <jaxrs:providers>
        <ref bean ="jsonProvider" />
    </jaxrs:providers>
</jaxrs:server>


来源:https://stackoverflow.com/questions/12998480/how-to-remove-namespace-prefixes-from-json-response-in-cxf-jax-rs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!