如何将包含jaxbelement的肥皂响应对象映射到另一个pojo?

发布于 2025-02-09 03:43:03 字数 2337 浏览 1 评论 0原文

我创建了一个JAX-WS客户端并进行肥皂调用。它可以正常工作,但是当将从SOAP调用作为JSON对象收到的响应返回时,我有问题。

这就是我调用SOAP服务并获取响应的方式:

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

问题是,GetUsersPonse是使用wsimport生成的,当我将此响应返回为JSON对象时,它具有很多不必要的领域。这是外观:

{
   "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
     }
   }
  }
}

但是我只想返回这样的字段值,例如:

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

我可以手动挖掘响应并提取这样的值,但是它需要很多代码,并且可能不是一个好练习。

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

有什么办法吗?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

被翻牌 2025-02-16 03:43:03

我找到了解决方案在这里。原因是,WSDL宣布这些字段既可选又可命运。这就是为什么我在生成的课程中有jaxbelement的原因。我使用wsimport命令的绑定文件并重新生成了类。

绑定文件:

<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>

和命令:

wsimport -keep  <WSDL_location>  -b binding.xml

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文