Changing a web service response using JAX-WS

How to change the response namespace as follows:

old answer:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ns2:GetAmountResponse xmlns:ns2="http://ws.dsi.otn.com/dab"> <etat>0</etat> <montant>500.0</montant> </ns2:GetAmountResponse> </soap:Body> </soap:Envelope> 

need a new answer:

 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <GetAmountResponse xmlns="http://ws.dsi.otn.com/dab"> <etat>0</etat> <montant>500.0</montant> </GetAmountResponse> </soap:Body> </soap:Envelope> 

I want to remove the ns2 namespce prefix.

+4
source share
2 answers

In the first case, GetAmountResponse is in the namespace http://ws.dsi.otn.com/dab , while etat and montant are in the empty namespace by default.

In the new desired post, GetAmountResponse , etat and montant are in the http://ws.dsi.otn.com/dab namespace.

Namespaces can be managed from the namespaces of your classes. Use the same namespace in all, and you will have them in the same namespace, leave the default classes and the default empty namespace.

For example, if you have something like this in your web service:

 @WebMethod public @WebResult(name = "getAmountResponse", targetNamespace = "http://ws.dsi.otn.com/dab") AmountResponse getAmount( @WebParam(name = "getAmountRequest", targetNamespace = "http://ws.dsi.otn.com/dab") AmountRequest request) { AmountResponse response = new AmountResponse(); response.setEtat(0); response.setMontant(500.0); return response; } 

with the response class as follows:

 @XmlRootElement public class AmountResponse { private int etat; private double montant; // getter and setters omitted } 

You will receive the first type of soap message.

But if you change the response class to look like this:

 @XmlRootElement(namespace = "http://ws.dsi.otn.com/dab") @XmlAccessorType(XmlAccessType.NONE) public class AmountResponse { @XmlElement(namespace = "http://ws.dsi.otn.com/dab") private int etat; @XmlElement(namespace = "http://ws.dsi.otn.com/dab") private double montant; // getters and setter omitted } 

you bring all the tags in one namespace and get something equivalent to the new type of message you want. I said the equivalent, because I don’t think you will get exactly this:

 <GetAmountResponse xmlns="http://ws.dsi.otn.com/dab"> <etat>0</etat> <montant>500.0</montant> </GetAmountResponse> 

Most likely, you will get something like this:

 <ns2:getAmountResponse xmlns:ns2="http://ws.dsi.otn.com/dab"> <ns2:etat>0</ns2:etat> <ns2:montant>500.0</ns2:montant> </ns2:getAmountResponse> 

This is the same "XML value" for both messages, although they do not look the same.

If you absolutely want this to look like this, I think you will have to go β€œlow” and use something like a SOAP handler to intercept the response and change it . But keep in mind that it will not be a trivial task to change the message before it goes to the wire.

+3
source

a logical handler is enough to convert it to the expected message:

 package com.ouertani.slim; import java.io.ByteArrayInputStream; import java.io.InputStream; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; import javax.xml.ws.LogicalMessage; import javax.xml.ws.handler.LogicalHandler; import javax.xml.ws.handler.LogicalMessageContext; import javax.xml.ws.handler.MessageContext; /** * * @author ouertani */ public class MyLogicalHandler implements LogicalHandler<LogicalMessageContext> { @Override public boolean handleMessage(LogicalMessageContext messageContext) { /// extract state and amount int state = 0; double amount = 200.0; transform(messageContext, state, amount); return false; } public boolean handleFault(LogicalMessageContext messageContext) { return true; } public void close(MessageContext context) { } private void transform( LogicalMessageContext messageContext, int etat, double montant){ LogicalMessage msg = messageContext.getMessage(); String htom = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"+ "<soap:Body>"+ "<GetAmountResponse xmlns=\"http://ws.dsi.otn.com/dab\">"+ "<etat>"+etat+"</etat>"+ "<montant>"+montant+"</montant>"+ "</GetAmountResponse>"+ "</soap:Body>"+ "</soap:Envelope>"; InputStream is = new ByteArrayInputStream(htom.getBytes()); Source ht = new StreamSource(is); msg.setPayload(ht); } } 
0
source

All Articles