Modify response of web service with JAX-WS

后端 未结 2 995
清酒与你
清酒与你 2020-12-18 08:31

How can I modify the namespace of the response like this:

old response:



        
2条回答
  •  有刺的猬
    2020-12-18 09:09

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

    In the new message you want, GetAmountResponse, etat and montant are all in namespace http://ws.dsi.otn.com/dab.

    The namespaces can be controlled from the namespaces of your classes. Use the same namespace in all and you will have them in the same namespace, leave classes with defaults and they default to empty namespace.

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

    @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 a response class like this:

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

    you will end up with the first type of soap message.

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

    @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 will bring all tags in the same namespace and you get something equivalent to the new type of message you want. I said equivalent because I don't think you will get exactly this:

    
         0
         500.0
    
    

    It's more likely to get something like this instead:

    
         0
         500.0
    
    

    It's the same "XML meaning" for both messages although they don't look the same.

    If you absolutely want it to look like that, I think you will have to go "low level" and use something like a SOAP handler to intercept the response and modify it. But be aware that it won't be a trivial task to change the message before it goes on the wire.

提交回复
热议问题