Cxf-rest configure JSonProvider to customize the JSON conversion format

Source: Internet
Author: User

About CXF JSonProvider
CXF supports JSon through JSonProvider. The default Provider uses jettsion for encoding or decoding.
Both the server side and the client side can configure the Json format by configuring JSonProvider.
For example
Java code:
<Bean id = "jsonProvider" class = "org. apache. cxf. jaxrs. provider. JSONProvider">
<Property name = "dropRootElement" value = "true"/>
<Property name = "dropCollectionWrapperElement" value = "true"/>
<Property name = "serializeAsArray" value = "true"/>
</Bean>

<Jaxrs: server id = "customerService" address = "/service1">
<Jaxrs: serviceBeans>
<Ref bean = "customerBean"/>
</Jaxrs: serviceBeans>
<Jaxrs: providers>
<Ref bean = "jsonProvider"/>
</Jaxrs: providers>
</Jaxrs: server>
 
The dropRootElement attribute of JsonProvider is false by default. The class name is used as the first node in Json format, as shown in figure
{Customer: {"id": 123, "name": "John"}. If it is set to true, the Json format is {"id": 123, "name ": "John"}, while Gson and other Json packages are parsed in the latter format.
The default dropCollectionWrapperElement attribute is false. In case of Collection, Json regards the class name in the Collection as a node, for example, {"Customer" :{{ "id": 123, "name": "John" }}, and set to false, the JSon format is {"id": 123, "name": "John "}}
The default value of serializeAsArray is false. In Collecion, the format is {"id": 123, "name": "John"}. If it is set to true, the format is [{"id": 123, "name": "john"}], and Gson is parsed as the latter.
 
For the CXF Client, JSonProvider is also set and can be directly encoded using spring.
Java code:
JSONProvider myJsonProvider = new JSONProvider ();
MyJsonProvider. setDropRootElement (true );
MyJsonProvider. setSupportUnwrapped (true );
MyJsonProvider. setSerializeAsArray (true );
List <JSONProvider> providers = new ArrayList <JSONProvider> ();
Providers. add (myJsonProvider );
WebClient client = WebClient. create ("http: // 10.148.11.102: 8080/testCXFServer", providers );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.