简体   繁体   中英

How to map soap response object containing JAXBElement to another POJO?

I created a jax-ws client and make soap calls. It works fine but I have problems when returning the response received from soap call as a json object.

This is how I call SOAP service and get the response:

@Override
public GetUserResponse getUser(Integer userId) {
    ObjectFactory objectFactory = new ObjectFactory();
    GetUserRequest getUserRequest = objectFactory.createGetUserRequest();
    getUserRequest.setUserId(userId);
    GetUserResponse response = getClient().getUser(getUserRequest);
    return response;
}

The problem is, GetUserResponse was generated using wsimport , and when I return this response as a JSON object, it has lots of unnecessary fields. Here is how it looks:

{
   "user":{
   "name":"{http://www.myservicedomain.com/service}User",
   "declaredType":"com.example.demo.User",
   "scope":"com.example.demo.GetUserResponse",
   "value":{
     "userId":{
        "name":"{http://www.myservicedomain.com/service}UserId",
        "declaredType":"java.lang.Integer",
        "scope":"com.example.demo.User",
        "value":88938134,
        "nil":false,
        "globalScope":false,
        "typeSubstituted":false
     },
     "name":{
        "name":"{http://www.myservicedomain.com/service}Name",
        "declaredType":"java.lang.String",
        "scope":"com.example.demo.User",
        "value":"",
        "nil":false,
        "globalScope":false,
        "typeSubstituted":false
     },
     "active":{
        "name":"{http://www.myservicedomain.com/service}Active",
        "declaredType":"java.lang.Boolean",
        "scope":"com.example.demo.User",
        "value":true,
        "nil":false,
        "globalScope":false,
        "typeSubstituted":false
     },
     "postalCode":{
        "name":"{http://www.myservicedomain.com/service}PostalCode",
        "declaredType":"java.lang.String",
        "scope":"com.example.demo.Address",
        "value":null,
        "nil":true,
        "globalScope":false,
        "typeSubstituted":false
     }
   }
  }
}

But I just want to return the values of the fields, like this:

{
   "user":{
     "userId": 88938134,
     "name": "My name",
     "active": true,
     "postalCode": null
   }
}

I can manually dig into response and extract values like this, but it requires a lot of code and probably not a good practice.

GetUserResponse response = getClient().getUser(getUserRequest);
User user = response.getUser().getValue();
JAXBElement<String> name = user.getName();
String nameValue = name.getValue();
//etc..

Is there any way to do that?

Thank you.

I found the solution here . The reason is, the WSDL declares the fields to be both optional and nillable. That is why I had JAXBElement in my generated classes. I used a binding file with wsimport command and regenerated the classes.

The binding file:

<jaxb:bindings version="2.1" 
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
   <jaxb:globalBindings generateElementProperty="false"/> 
</jaxb:bindings>

and the command:

wsimport -keep  <WSDL_location>  -b binding.xml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM