使用 JAX-WS 修改 Web 服务的响应 [英] Modify response of web service with JAX-WS

查看:36
本文介绍了使用 JAX-WS 修改 Web 服务的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何像这样修改响应的命名空间:

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

旧回复:

<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>

需要新的回复:

<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>

我想删除 ns2 namespce 前缀.

I want to remove the ns2 namespce prefix.

推荐答案

在第一种情况下,GetAmountResponse 在命名空间 http://ws.dsi.otn.com/dabetatmontant 位于默认(空)命名空间中.

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.

在你想要的新消息中,GetAmountResponseetatmontant 都在命名空间http://ws.dsi.otn.com/dab.

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.

例如,如果您的 Web 服务类中有这样的内容:

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;
    }

具有这样的响应类:

@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:

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

更有可能得到这样的结果:

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

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

这两条消息的XML 含义"相同,尽管它们看起来不一样.

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

如果你绝对希望它看起来像那样,我认为你将不得不去低级"并使用类似一个 SOAP 处理程序来拦截响应并修改它.但请注意,在消息传输之前更改消息绝非易事.

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.

这篇关于使用 JAX-WS 修改 Web 服务的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆