#XmlElementWrapper for web method using JAX-WS - java

I have a web service like below, it contains a web method which will return a list of objects:
#WebService(name = "ClubMembershipPortType", serviceName = "ClubMembershipService", portName = "ClubMembershipSoapPort", targetNamespace = "http://club.com/api/ws")
public class ClubMembershipWS {
#WebMethod(operationName = "findClubMembershipsByClubId", action = "urn:findClubMembershipsByClubId")
#WebResult(name = "club_membership")
public List<ClubMembership> findClubMembershipsByClubId(#XmlElement(required=true)
#WebParam(name = "club_id") String clubId,
#WebParam(name = "status") StatusEnum status)
...
...
}
}
The response I got for the api request is like below:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:findClubMembersByClubIdResponse xmlns:ns2="http://club.com/api/ws">
<club_membership>
...
</club_membership>
<club_membership>
...
</club_membership>
</ns2:findClubMembersByClubIdResponse>
</S:Body>
</S:Envelope>
The question is how to use #XmlElementWrapper (or other way?) to make the response like below?
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:findClubMembersByClubIdResponse xmlns:ns2="http://club.com/api/ws">
<club_membership_list>
<club_membership>
...
</club_membership>
<club_membership>
...
</club_membership>
</club_membership_list>
</ns2:findClubMembersByClubIdResponse>
</S:Body>
</S:Envelope>

Did you try this?
#XmlElementWrapper(name="club_membership_list", required=true)
#XmlElement(name="club_membership", required=true)
public List<ClubMembership> findClubMembershipsByClubId(#WebParam(name = "club_id") String clubId,
#WebParam(name = "status") StatusEnum status)

Annotate your method with:
#WebResult(name="club_membership_list", targetNamespace = "http://club.com/api/ws")

Related

Java WS get SOAP request CDATA body

I need to get the following SOAP request into my controller.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<req:ResultMsg
xmlns:req="http://cps.huawei.com/cpsinterface/result"><![CDATA[
<?xml version="1.0" encoding="UTF-8"?>
<Result>
<ResultType>0</ResultType>
<ResultCode>0</ResultCode>
<ResultDesc>The service request is processed successfully.</ResultDesc>
<OriginatorConversationID>S_X2013012921001</OriginatorConversationID>
<ConversationID>AG_20130129T102103</ConversationID>
<TranactionID>XD2013012923789234</TranactionID>
<ResultParameters></ResultParameters>
</Result>]]></req:ResultMsg>
</soapenv:Body>
</soapenv:Envelope>
I tried to set a POJO and a string for the CDATA part but not working.
Current implementation as below,
Interface:
#WebService(name = "VCashCallbackService",
targetNamespace = "http://cps.huawei.com/cpsinterface/result")
public interface VCashCallbackService {
#WebMethod(operationName = "ResultMsg")
#WebResult(name = "GetDataResponse")
GetDataResponse getData(#WebParam(name = "ResultMsg",
targetNamespace = "http://cps.huawei.com/cpsinterface/resul",
mode = WebParam.Mode.IN) Holder<String> searchResultDataXML);
}
Impl
#WebService(serviceName = "VCashCallbackService",
portName = "VCashCallbackServicePort",
endpointInterface = "com.argon.eightd.web.restful.mock.VCashCallbackService",
targetNamespace = "http://cps.huawei.com/cpsinterface/result")
#BindingType(value = javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
#Service
public class VCashCallbackServiceImpl implements VCashCallbackService {
private static final Logger LOGGER = LoggerFactory.getLogger(VCashCallbackServiceImpl.class);
#Override
public GetDataResponse getData(Holder<String> searchResultDataXML) {
LOGGER.info("SOAP result: {}", searchResultDataXML.searchResultDataXML.value);
return new GetDataResponse();
}
}
Log
SOAP result: null
But this approach is getting the request as above in the WebMethod.
Thanks in advance.

How to read the header in the web server response

Tell me how to read the header in the web server's response via the #WebMethod and #WebResult annotations. Of course, I can do this by SOAPConnection and parsing SOAPMessage, but there is a lot of functionality on javax.jws and I would like to unify everything. I need value from <osb:Backend/>.
Server response:
<soapenv:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<osb:Backend xmlns:osb="http://osb.emias.mos.ru/system">СКУУ</osb:Backend>
<ipaddr xmlns="https:/bis.skyy.soapHeader/">10.0.5.147</ipaddr>
<build xmlns="https:/bis.skyy.soapHeader/">1ec22a8</build>
</env:Header>
<env:Body xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<getEmployeePacketInfoResponse2 xmlns="http://emias.gov.ru/medempregisterservicetypes/1" xmlns:ns1="http://emias.gov.ru/types/1" xmlns:ns0="http://emias.gov.ru/servicetypes/1">
<EmployeeList>
.......
Interface declaration:
#WebResult(name = "Backend", targetNamespace = "http://emias.gov.ru/medempregisterservicetypes/1", partName = "getMedicalEmployeePacketInfo")
#WebMethod
public String getMedicalEmployeePacketInfo2(
#WebParam(partName = "getMedicalEmployeePacketInfoRequest", name = "getEmployeePacketInfoRequest", targetNamespace = "http://emias.gov.ru/medempregisterservicetypes/1")
GetEmployeePacketInfoRequest getMedicalEmployeePacketInfoRequest
) throws FaultMessage;
Request class:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {
"requesterSystemCode",
"healthOrgID",
"employeeList"
})
#XmlRootElement(name = "getEmployeePacketInfoRequest")
public class GetEmployeePacketInfoRequest {
#XmlElement(name = "RequesterSystemCode", required = true)
protected String requesterSystemCode;
#XmlElement(name = "HealthOrgID")
protected String healthOrgID;
#XmlElement(name = "EmployeeList", required = true)
protected GetEmployeePacketInfoRequest.EmployeeList employeeList;
....
Request example:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://emias.gov.ru/medempregisterservicetypes/1">
<soapenv:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username>SPU/erz</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">emias_erz</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<ns:getEmployeePacketInfoRequest2>
<ns:RequesterSystemCode>SPU</ns:RequesterSystemCode>
<ns:HealthOrgID>10000430</ns:HealthOrgID>
<ns:EmployeeList>
<ns:EmployeeID>21426012</ns:EmployeeID>
</ns:EmployeeList>
</ns:getEmployeePacketInfoRequest2>
</soapenv:Body>
</soapenv:Envelope>
1. I haven't used jws, and haven't tried this, but it may help:
Try to add header option to your #WebResult annotation.
See https://docs.oracle.com/javaee/6/api/javax/jws/WebResult.html :
public abstract boolean header
"If true, the result is pulled from a message header rather then the message body."
But seems like in order to do so you have to write appropriate class for JAXB (I'm not sure).
2. Instead, I did it straightforward, and used this method:
Your business data is located inside message body - between <getEmployeePacketInfoRequest2/> tags. This is what JAXB creates your GetEmployeePacketInfoResponse class objects from. So, if you want to obtain anything outside this tags, you have to get it from the whole SOAP response. But you don't need to parse it manually - javax.xml.soap.SOAPMessage has inbuilt getSOAPHeader() method, which does what you need. Then just transform it to DOM, and do getElementsByTagName() or getElementsByTagNameNS().
implemented the second sentence
import lombok.val;
....
val soapPart = resp.getSOAPPart();
val soapEnvelope = soapPart.getEnvelope();
val soapHeader = soapEnvelope.getHeader();
val backendNode = soapHeader.getElementsByTagName("osb:Backend");
if (backendNode.getLength() > 0) {
backend = backendNode.item(0).getTextContent();
}
...
You need to use annotated parameter in your class method like this:
#WebResult(name = "Backend", targetNamespace = "http://emias.gov.ru/medempregisterservicetypes/1", partName = "getMedicalEmployeePacketInfo")
#WebMethod
public String getMedicalEmployeePacketInfo2(
#WebParam(partName = "header", name = "ipaddr", targetNamespace = "https:/bis.skyy.soapHeader/", header = true, mode = WebParam.Mode.IN)
String header,
...
#WebParam(partName = "getMedicalEmployeePacketInfoRequest", name = "getEmployeePacketInfoRequest", targetNamespace = "http://emias.gov.ru/medempregisterservicetypes/1")
GetEmployeePacketInfoRequest getMedicalEmployeePacketInfoRequest
) throws FaultMessage;
pay attention to header = true and mode = WebParam.Mode.IN

remove a tag in soap response using CXF

I am new to CXF. I am using CXF component in mulesoft to create Webservice. WebService is running successfully. But, I want to remove a tag from response.
I have used #ResponseWrapper, #SoapBinding(ParameterStyle=ParameterStyle.BARE). but, these are not resolved my issue.
I have heared that, we can modify soap response(i.e remove tag) by using Outintercepters. If it is can anybody help me to how to use interceptors and what phase we can modify the soap response to remove tag..
Actual Soap Response
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope">
<soap:Body>
<ns2:getMyresponse xmlns:ns2="http://myschema.com">
<return>
<errorcode>1</errorcode>
<errormsg>notsuccesful</errorms>
</return>
</ns2:getMyresponse>
</soap:Body>
</soap:Evelope>
Expected Response
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope">
<soap:Body>
<ns2:getMyresponse xmlns:ns2="http://myschema.com">
<errorcode>1</errorcode>
<errormsg>notsuccesful</errorms>
</ns2:getMyresponse>
</soap:Body>
</soap:Evelope>
SEI class is:
#WebService(targetNamespace = "urn:com.test", name = "GetActivityListInterface")
public interface GetActivityListInterface {
#SOAPBinding(parameterStyle = ParameterStyle.BARE)
#RequestWrapper(localName = "GetMyActivities", targetNamespace = "urn:com.test", className = "com.test.beans.MyActivities")
#WebMethod(operationName = "GetMyActivities", action = "urn:com.test/GetMyActivities")
#ResponseWrapper(localName = "GetMyResponse", targetNamespace = "urn:com.test", className = "com.test.beans.GetMyResponse")
public GetMyActivitiesResponse getMyActivities(
#WebParam(name = "id", partName="id")
java.lang.String id,
#WebParam(name = "date", partName="date")
java.lang.String date);
}
Use an XSL-T to remove the tags that you want to remove from response xml.
Cheers!
Remove the #ResponseWrapper and use #WebResult. Like this
#WebService(targetNamespace = "urn:com.test", name = "GetActivityListInterface")
public interface GetActivityListInterface {
#SOAPBinding(parameterStyle = ParameterStyle.BARE)
#RequestWrapper(localName = "GetMyActivities", targetNamespace = "urn:com.test", className = "com.test.beans.MyActivities")
#WebMethod(operationName = "GetMyActivities", action = "urn:com.test/GetMyActivities")
#WebResult(name = "GetMyResponse", partName="GetMyResponse", targetNamespace = "urn:com.test")
public GetMyActivitiesResponse getMyActivities(
#WebParam(name = "id", partName="id")
java.lang.String id,
#WebParam(name = "date", partName="date")
java.lang.String date);
}

SOAPBinding.ParameterStyle.BARE vs SOAPBinding.ParameterStyle.WRAPPED : generated less parameters on request

I use SOAPUI and generate java classes with JAX-WS import.
I have an interface like this
#WebService(name = "test", targetNamespace = "http://lang.java")
#SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
#XmlSeeAlso({
ObjectFactory.class
})
public interface Test{
#WebMethod(action = "https://...action")
#WebResult(name = "getBean", targetNamespace = "http://...getBean", partName = "getBean")
public Bean test(
#WebParam(name = "parameter1", targetNamespace = "http://lang.java", partName = "parameter1")
String parameter1,
#WebParam(name = "parameter2", targetNamespace = "http://lang.java", partName = "parameter2")
String parameter2,
#WebParam(name = "parameter3", targetNamespace = "http://lang.java", partName = "parameter3")
String parameter3,
#WebParam(name = "parameter4", targetNamespace = "http://lang.java", partName = "parameter4")
long parameter4);
}
If I use SOAPBinding.ParameterStyle.WRAPPED the body message generated is
<S:Body>
<ns2:test xmlns:ns2="http://lang.java" xmlns:ns3="http://...getBean">
<ns2:parameter1>1</ns2:parameter1>
<ns2:parameter2>2</ns2:parameter2>
<ns2:parameter3>a</ns2:parameter3>
<ns2:parameter4>1</ns2:parameter4>
</ns2:test>
</S:Body>
If I use SOAPBinding.ParameterStyle.BARE the body message generated is
<S:Body>
<ns2:parameter1 xmlns:ns2="http://lang.java" xmlns:ns3="http://...getBean">1</ns2:parameter1>
</S:Body>
Why is the diference? Why in Bare option it only generates the first parameter? I need that Bare option create all parameters
Its ok! I find the answer here http://www.javajee.com/soap-binding-style-encoding-and-wrapping
Bare option only can use one parameter. When we use Bare, the message request must have zero or one element into Body. The solution is make an object with all parameters we want , and send this object to the method.

Weblogic Webservices - In SOAP response - GT resolves to left bracket but LT does not (stays as <)?

I have a Webservice that returns an XML Payload.
In that Web Service Response - My GT brackets resolve to a real bracket - while the LT stays as < - i have no idea why.
Using
Weblogic 10.3
The Webservice is annotated as
#WebService(name = "MyService", portName = "MyServicePort", serviceName = "MyService", targetNamespace = "http://kwikksilva/myservice/ws")
#SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
The method like so
#WebMethod
#WebResult(name = "SyncResponse", targetNamespace = "http://kwikksilva/myservice/ws")
public SyncResponseTO processRequest(
It returns a TO with a payload
#XmlRootElement(name = "SyncResponse", namespace = "http://kwikksilva/myservice/ws")
#XmlAccessorType(XmlAccessType.FIELD)
public class SyncResponseTO implements Serializable {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 1L;
/** The response. */
#XmlElement(name = "Payload", nillable = false, required = true)
private String payload;
I get a response back which looks like this
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:processSyncRequestResponse xmlns:ns2="http://kwikksilva/myservice/ws">
<ns2:SyncResponse>
<Payload xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">
<Vbc>
<ApptEffDt>795243600000</ApptEffDt>
</Vbc>
</Payload>
</ns2:SyncResponse>
</ns2:processSyncRequestResponse>
</S:Body>
</S:Envelope>
Why would one encode to a bracket and not the other?
Has anyone got any ideas on this - i am confused....
take the payload and xml decode it. It should solve the problem.

Categories