JAX-WS fault: throw superclass of exception - java

I want to throw subclasses of MyCustomException but for the superclass to be transferred across the web service; however, the subclass is transferred, instead. I have tried adding the annotation WebFault to the classes with no effect. I have provided an example of what is currently happening and an example of what I want to happen instead.
The exceptions.
public class MyCustomException extends Exception {
String text;
public static class CustomInner extends MyCustomException {
public CustomInner1() {
super("inner");
}
}
public MyCustomException(String text) {
this.text = text;
}
public String getText() {
return text;
}
}
The web service implementation. NB: I don't want to change what is being thrown here.
#Stateless(name = "MyService", mappedName = "MyService")
#LocalBean
#WebService(targetNamespace = "http://my.org/ns/")
public class MyService {
#WebMethod
public String throwCustomInnerException() throws MyCustomException {
throw new MyCustomException.CustomInner();
}
#WebMethod
public String throwCustomException() throws MyCustomException {
throw new MyCustomException("text");
}
}
The XML for throwCustomException() call using the web service.
<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope">
<faultcode>S:Server</faultcode>
<faultstring>pack.MyCustomException</faultstring>
<detail>
<ns2:MyCustomException xmlns:ns2="http://my.org/ns/">
<text>text</text>
</ns2:MyCustomException>
</detail>
</S:Fault>
</S:Body>
</S:Envelope>
The XML for throwCustomInnerException() call using the web service.
<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope">
<faultcode>S:Server</faultcode>
<faultstring>pack.CustomInner</faultstring>
</S:Fault>
</S:Body>
</S:Envelope>
What I want to happen is the following, when throwCustomInnerException() is called using the web service:
<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope">
<faultcode>S:Server</faultcode>
<faultstring>pack.MyCustomException</faultstring>
<detail>
<ns2:MyCustomException xmlns:ns2="http://my.org/ns/">
<text>inner1</text>
</ns2:MyCustomException>
</detail>
</S:Fault>
</S:Body>
</S:Envelope>

You can change the method:
#WebMethod
public String throwCustomInnerException() throws MyCustomException {
throw new MyCustomException.CustomInner();
}
to:
#WebMethod
public String throwCustomInnerException() throws MyCustomException {
throw new MyCustomException(CustomInner.getClass().getSimpleName());
}

Related

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>

How to remove namespace xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

I have this on my request soapenv:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
I want to remove xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Can I do that from my Service_BindingStub?
Maybe can set org.apache.axis.client.Call object with some property... I don't know.
If your issue to have xsi namespace in the root so extend SoapSerializationEnvelope and then override method: write like:
#Override
public void write(XmlSerializer writer) throws IOException {
// writer.setPrefix("i", this.xsi);
writer.setPrefix("d", this.xsd);
writer.setPrefix("c", this.enc);
writer.setPrefix("v", this.env);
writer.startTag(this.env, "Envelope");
writer.startTag(this.env, "Header");
this.writeHeader(writer);
writer.endTag(this.env, "Header");
writer.startTag(this.env, "Body");
this.writeBody(writer);
writer.endTag(this.env, "Body");
writer.endTag(this.env, "Envelope");
}

Setting up an XML response for SOAP

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

Changing JAX-WS default XML namespace prefix

I've generated source codes for an old Web Service using JAX-WS 2.1.7. When I call this service the generated soap message is something like this:
<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>
<env:Header>
</env:Header>
<env:Body>
...
</env:Body>
</env:Envelope>
But the old web service only accepts this format:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
...
</soap:Body>
</soap:Envelope>
As you see the prefix is "soap" instead "env" and there is no header so i got an error complaining about "soap:Body" is required. I can't change the old web service and need to send compatible soap messages. how can i change the prefix to "soap" and also remove "Header"?
You need to create a class that implements SOAPHandler<SOAPMessageContext> and that includes something like this:
public boolean handleMessage(final SOAPMessageContext context)
{
final Boolean isSoapResponse = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (!isSoapResponse)
{
try
{
final SOAPMessage soapMsg = context.getMessage();
soapMsg.getSOAPPart().getEnvelope().setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:soap", "http://schemas.xmlsoap.org/soap/envelope/");
soapMsg.getSOAPPart().getEnvelope().removeAttributeNS("http://schemas.xmlsoap.org/soap/envelope/", "env");
soapMsg.getSOAPPart().getEnvelope().removeAttribute("xmlns:env");
soapMsg.getSOAPPart().getEnvelope().setPrefix("soap");
soapMsg.getSOAPBody().setPrefix("soap");
soapMsg.getSOAPPart().getEnvelope().getHeader().detachNode();
}
catch (SOAPException e)
{
e.printStackTrace();
}
}
return true;
}
Then create a handler.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<handler-chains xmlns="http://java.sun.com/xml/ns/javaee">
<handler-chain>
<handler>
<handler-name>test.MySoapHandler</handler-name>
<handler-class>test.MySoapHandler</handler-class>
</handler>
</handler-chain>
</handler-chains>
And add an annotation to your web service:
#HandlerChain(file = "handler.xml")

Categories

Resources