Avoid wrapping object type name from JSON I / O (CXF web service)

I have a CXF web service something like this:

@Service("MyWebService") public class MyWebService implements IMyWebService { @Autowired private IMyService MyService; public ResponseObject doSomething(RequestObject requestObject) { ResponseObject responseObject = new ResponseObject; . // do something.... . . return responseObject; } } 

which is expecting JSON input, say something like this:

 { "requestObject" : { "amount" : 12.50, "userName" : "abcd123" } } 

and outputs the output JSON like this:

 { "responseObject" : { "success" : "true", "errorCode" : 0 } } 

Is there a way to configure CXF so that it accepts input JSON in the following format:

 { "amount" : 12.50, "userName" : "abcd123" } 

I need to cross out the object type name 'requestObject' / 'responseObject' in the input and output JSON. Is it possible?

Your help was appreciated!

+6
source share
2 answers

If you are using maven, the JSONProvider class is here:

 <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-rs-extension-providers</artifactId> <version>2.7.5</version> </dependency> 

You may need other json provider properties to achieve your goals:

 <jaxrs:providers> <bean class="org.apache.cxf.jaxrs.provider.json.JSONProvider"> <property name="dropRootElement" value="true"/> <property name="dropCollectionWrapperElement" value="true"/> <property name="serializeAsArray" value="true"/> <property name="supportUnwrapped" value="true"/> </bean> </jaxrs:providers> 
+5
source

If you are setting up the json provider through the springs xml configuration file (e.g. applicationContext.xml), just add the configuration below to make it work.

 <jaxrs:providers> <bean class="org.apache.cxf.jaxrs.provider.json.JSONProvider"> <property name="dropRootElement" value="true" /> <property name="supportUnwrapped" value="true" /> </bean> </jaxrs:providers> 

DropRootElement tells the json provider to reset the root element. Refer to this JSON Support for more customization and understanding.

+1
source

Source: https://habr.com/ru/post/916428/


All Articles