Setting up an XML response for SOAP - java

I'm having a real hard time with this issue.
Basically I've built a WebService using Java/JAX-WS, this is my Interface
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
#WebService
public interface OperationsInterface {
#WebResult(name = "result")
LoginResponse Login(#WebParam(name = "login") Login login);
#WebResult(name = "result")
String Purchase(#WebParam(name = "purchase") Purchase purchase);
}
Login is just a POJO that contains 2 strings (username and password)
LoginResponse is another POJO, it has this
String status;
int code;
String message;
SubscriptionTier subscriptionTier;
String accountNumber;
String firstName;
String lastName;
Nothing fancy, pretty simple in fact. The request is something like this
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ott="http://ott.amentum.net/">
<soapenv:Header/>
<soapenv:Body>
<ott:Login>
<login>
<username>USERNAME</username>
<password>1234</password>
</login>
</ott:Login>
</soapenv:Body>
</soapenv:Envelope>
And the response comes like this
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:LoginResponse xmlns:ns2="http://ott.amentum.net/">
<result>
<accountNumber>0110000076</accountNumber>
<code>1</code>
<firstName>JOHN</firstName>
<lastName>DOE</lastName>
<message>Login has been successful.</message>
<status>success</status>
<subscriptionTier>
<tier>TVOD</tier>
<tier>CANAL</tier>
<tier>CATCHUP</tier>
</subscriptionTier>
</result>
</ns2:LoginResponse>
</soap:Body>
</soap:Envelope>
As far as I know, this is correct, however, my customer is expecting something like this
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:LoginResponse xmlns:ns2="http://ott.amentum.net/">
<accountNumber>0410089676</accountNumber>
<code>1</code>
<firstName>MARIA DEL CARMEN</firstName>
<lastName>PADILLA MARTIN</lastName>
<message>Login has been successful.</message>
<status>success</status>
<subscriptionTier>
<tier>TVOD</tier>
<tier>CANAL</tier>
<tier>CATCHUP</tier>
</subscriptionTier>
</ns2:LoginResponse>
</soap:Body>
</soap:Envelope>
They want me to remove the result tag, which is the name of the LoginResponse object I created to return the information.
Is there a way to do this?
Thanks in advance

Related

How do I call a request that has entity in it?

(1) The class I described below includes three strings and one entity.
(2) The text I sent as a call is as follows.
(3) But I'm making the mistake.
1:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "", propOrder = {"tesisatKod", "islemGuid", "islemTutari", "islemTuru"})
#XmlRootElement(name = "secondStageRequest")
public class SecondStageRequest {
protected String tesisatKod;
protected String islemGuid;
// protected Date sorguSaatTarih;
protected String islemTutari;
protected IslemTuru islemTuru;
}
2:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xc="http://xml.cinigaz.com.tr">
<soapenv:Header>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" wsse:mustUnderstand="1">
<wsse:UsernameToken>
<wsse:Username>admin</wsse:Username>
<wsse:Password>pwd1234</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<xc:secondStageRequest>
<xc:tesisatKod>bbb</xc:tesisatKod>
<xc:islemGuid>bbb</xc:islemGuid>
<xc:islemTutari>bbb</xc:islemTutari>
<xc:islemTuru>
<xc:islemTuruId> 1</xc:islemTuruId>
<xc:islemTuruAdi> aaa</xc:islemTuruAdi>
</xc:islemTuru>
</xc:secondStageRequest>
</soapenv:Body>
</soapenv:Envelope>
3:
WARN 20148 --- [nio-9020-exec-2] s.w.s.s.e.i.PayloadValidatingInterceptor : XML validation error on request: cvc-type.3.1.2: Element 'xc:islemTuru' is a simple type, so it must have no element information item [children].

How to change namespace prefix in SOAP webservice request created with SOAPMessage?

When I create my SOAP request using the javax.xml.soap.SOAPMessage class, I get a namespace prefix in my XML like <SOAP-ENV:Envelope>.
Is there a simple way to change the namespace prefix to <soapenv:Envelope>?
The XML namespace prefix should not matter as long as you are calling a well behaved web service. So SOAP-ENV:, soapenv:, or whatever: are basically the same thing as long as they both refer to the same namespace.
With that being said, if you really need to do this for some reason, here is some minimally working code. I'm calling this service.
package soap;
import java.io.IOException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
public class Client {
public static void main(String[] args) throws SOAPException, IOException {
MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();
MimeHeaders mimeHeader = message.getMimeHeaders();
mimeHeader.setHeader("SOAPAction", "http://tempuri.org/Add");
SOAPBody body = message.getSOAPBody();
SOAPHeader header = message.getSOAPHeader();
QName bodyName = new QName("http://tempuri.org/", "Add", "tmp");
SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
SOAPElement intA = bodyElement.addChildElement(new QName("http://tempuri.org/", "intA", "tmp"));
intA.addTextNode("123");
SOAPElement intB = bodyElement.addChildElement(new QName("http://tempuri.org/", "intB", "tmp"));
intB.addTextNode("456");
message.writeTo(System.out);
System.out.println();
// -----------------> Now changing the prefix before making the call
SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
envelope.removeNamespaceDeclaration(envelope.getPrefix());
String prefix = "soapenv";
envelope.addNamespaceDeclaration(prefix, "http://schemas.xmlsoap.org/soap/envelope/");
envelope.setPrefix(prefix);
header.setPrefix(prefix);
body.setPrefix(prefix);
// <---------------- done changing the prefix
message.writeTo(System.out);
System.out.println();
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = soapConnectionFactory.createConnection();
URL endpoint = new URL("http://www.dneonline.com/calculator.asmx");
SOAPMessage response = connection.call(message, endpoint);
connection.close();
response.writeTo(System.out);
}
}
I've printed the request message before and after the transformation. The output is as follows (formatting not included :D):
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<tmp:Add xmlns:tmp="http://tempuri.org/">
<tmp:intA>123</tmp:intA>
<tmp:intB>456</tmp:intB>
</tmp:Add>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<tmp:Add xmlns:tmp="http://tempuri.org/">
<tmp:intA>123</tmp:intA>
<tmp:intB>456</tmp:intB>
</tmp:Add>
</soapenv:Body>
</soapenv:Envelope>
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<AddResponse xmlns="http://tempuri.org/">
<AddResult>579</AddResult>
</AddResponse>
</soap:Body>
</soap:Envelope>

Return an object on wsdl response

This is part of that question.
I want to integrate one system and the system strong required input/output params. The system works by wsdl. That's why I created a web-service on java :
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import java.util.Date;
#SOAPBinding(style=SOAPBinding.Style.DOCUMENT)
public class WebServices {
#WebMethod
public PerformTransactionResult Test2(){
PerformTransactionResult performTransactionResult = new PerformTransactionResult();
performTransactionResult.setErrorMsg("test");
return performTransactionResult;
}
}
my PerformTransactionResult class is:
import org.apache.cxf.aegis.type.java5.XmlType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "PerformTransactionResult")
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "PerformTransactionResult")
public class PerformTransactionResult {
private String errorMsg;
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
}
System, which I'm integrating want to get response like that:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<uws:PerformTransactionResult xmlns:uws="http://uws.provider.com/">
<errorMsg>Ok</errorMsg>
</uws:PerformTransactionResult>
</s:Body>
</s:Envelope>
My web-service is getting response:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns1:Test2Response xmlns:ns1="http://wservices.myhost.lan/">
<return xmlns:ns2="http://wservices.myhost.lan/">
<errorMsg>test</errorMsg>
</return>
</ns1:Test2Response>
</soap:Body>
</soap:Envelope>
As you see, response should return PerformTransactionResult, not Test2Response. How to I implement that task ?

How to handle multiple SOAP requests in the same envelope using Java Web Service?

I currently have a web service with the method:
#Override
#WebResult(Name="OIPResponse")
public Map<String, Object> getOIP(#WebParam(name = "invoice") String invoiceNumber,#WebParam(name = "part") String partNumber)
The normal SOAP request I use to call it looks like this:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:oip="http://oip.mycompany.com/">
<soapenv:Header/>
<soapenv:Body>
<oip:getOIP>
<invoice>41587182</invoice>
<part>9ZF2A5-570</part>
</oip:getOIP>
</soapenv:Body>
</soapenv:Envelope>
and the response is something like this:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:getOIPResponse xmlns:ns2="http://oip.mycompany.com/">
<OIPResponse>
<entry>
<key>ERR_CODE</key>
</entry>
<entry>
<key>SELLING_OU</key>
<value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">null</value>
</entry>
...
</OIPResponse>
</ns2:getOIPResponse>
</soap:Body>
</soap:Envelope>
I've tested this and it works fine. Now I am wondering if there is a way to include multiple requests in the same SOAP envelope like this:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:oip="http://oip.mycompany.com/">
<soapenv:Header/>
<soapenv:Body>
<oip:getOIP>
<invoice>41587182</invoice>
<part>9ZF2A5-570</part>
</oip:getOIP>
<oip:getOIP>
<invoice>41587183</invoice>
<part>9ZF2A5-570</part>
</oip:getOIP>
<oip:getOIP>
<invoice>41587184</invoice>
<part>9ZF2A5-570</part>
</oip:getOIP>
<oip:getOIP>
<invoice>41587185</invoice>
<part>9ZF2A5-570</part>
</oip:getOIP>
</soapenv:Body>
</soapenv:Envelope>
and get back something like this:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:getOIPResponse xmlns:ns2="http://oip.mycompany.com/">
<OIPResponse>
...
</OIPResponse>
<OIPResponse>
...
</OIPResponse>
<OIPResponse>
...
</OIPResponse>
...
</ns2:getOIPResponse>
</soap:Body>
</soap:Envelope>
Is there any way to do this?
I ended up creating a POJO called OIPRequest that contained the two parameters I need (invoice and part number) and a new method getOIPMultiple which takes an array of OIPRequests as an input parameter.
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
public class OIPRequest {
public String invoiceNumber, partNumber;
public OIPRequest(String invoice, String part) {
invoiceNumber = invoice;
partNumber = part;
}
public OIPRequest() {
invoiceNumber = "";
partNumber = "";
}
}
This makes the request look like this:
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Body>
<getMultipleOIP xmlns="http://oip.mycompany.com/">
<OIPRequest xmlns="">
<invoiceNumber>41587182</invoiceNumber>
<partNumber>9ZF2A5-570</partNumber>
</OIPRequest>
<OIPRequest xmlns="">
<invoiceNumber>41587182</invoiceNumber>
<partNumber>9ZF2A5-570</partNumber>
</OIPRequest>
<OIPRequest xmlns="">
<invoiceNumber>41587182</invoiceNumber>
<partNumber>9ZF2A5-570</partNumber>
</OIPRequest>
</getMultipleOIP>
</Body>
</Envelope>

SOAP request modification

I have a SOAP request like this, it's working fine:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://com/">
<soapenv:Header/>
<soapenv:Body>
<web:ConversionRate>
<!--Optional:-->
<FromCurrency>?</FromCurrency>
<!--Optional:-->
<ToCurrency>?</ToCurrency>
</web:ConversionRate>
</soapenv:Body>
</soapenv:Envelope>
I was changing the request a litle bit to understand the concepts:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" >
<soapenv:Header/>
<soapenv:Body>
<ConversionRate xmlns="http://com/">>
<!--Optional:-->
<FromCurrency>?</FromCurrency>
<!--Optional:-->
<ToCurrency>?</ToCurrency>
</ConversionRate>
</soapenv:Body>
</soapenv:Envelope>
The second one is not working, throwing a wrong answer.
My service class is
package com;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
#WebService (targetNamespace="http://com/")
public class CurrencyConvertor
{
public String ConversionRate (#WebParam(name = "FromCurrency") String FromCurrency, #WebParam(name = "ToCurrency") String ToCurrency)
{
System.out.println("ST\n" + FromCurrency + "\n" + ToCurrency + "\nEnd" );
switch(FromCurrency+","+ToCurrency)
{
case "USD,INR":
return "58";
case "INR,USD":
return "0.017";
default:
return "XXX";
}
}
}
The second request always falling to default case, the thing is, the values are sending as null since I changed the name space. So my Web service should answer for the second request properly, what should cause the issue, how to rectify this.
your namespace is not correct even it looks well.
I have to change com to com.example, because it is not possible to post answer with link to com only.
tns=http://com.example/ is defined in
the WebService, not for the webmethod. Change your method declaration to
public String ConversionRate (
#WebParam(name = "FromCurrency", tagetNamespace = "http://com.example/") String FromCurrency,
#WebParam(name = "ToCurrency", tagetNamespace = "http://com.example/") String ToCurrency) {
...
}
Even I'm not definitely sure if the XML has valid format
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" >
<soapenv:Header/>
<soapenv:Body>
<ConversionRate xmlns="http://com.example/">
<FromCurrency>?</FromCurrency>
<ToCurrency>?</ToCurrency>
</ConversionRate>
</soapenv:Body>
</soapenv:Envelope>
or namespace is required just for parameters
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" >
<soapenv:Header/>
<soapenv:Body>
<ConversionRate>
<FromCurrency xmlns="http://com.example/">?</FromCurrency>
<ToCurrency xmlns="http://com.example/">?</ToCurrency>
</ConversionRate>
</soapenv:Body>
</soapenv:Envelope>

Categories

Resources