Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid wrapping the object type name from input/output JSON (CXF Web Service)

Tags:

json

cxf

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!

like image 677
Higher-Kinded Type Avatar asked May 23 '12 07:05

Higher-Kinded Type


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 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>
like image 172
Stanislav Parkhomenko Avatar answered Nov 11 '22 01:11

Stanislav Parkhomenko


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.

like image 21
Sikorski Avatar answered Nov 11 '22 00:11

Sikorski