如何将包含jaxbelement的肥皂响应对象映射到另一个pojo?
我创建了一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了解决方案在这里。原因是,WSDL宣布这些字段既可选又可命运。这就是为什么我在生成的课程中有jaxbelement的原因。我使用
wsimport
命令的绑定文件并重新生成了类。绑定文件:
和命令:
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:
and the command: