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;
}
}
that expects an input JSON, say something like this:
{ "requestObject" : { "amount" : 12.50, "userName" : "abcd123" } }
and produces an output JSON something like this:
{ "responseObject" : { "success" : "true", "errorCode" : 0 } }
Is there a way to configure CXF such that it accepts the input JSON in the following format:
{ "amount" : 12.50, "userName" : "abcd123" }
I need to strip out the object type name 'requestObject' / 'responseObject' in the input and output JSON. Is that even possible?
Your help appreciated!
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 might need another 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>
If you are configuring the json provider through springs xml config file (like applicationContext.xml), then just add the below configuration it will 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>
The dropRootElement tells the json provider to drop the root element. Refer to this JSON Support for more configuration and understanding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With