簡體   English   中英

如何設置JAX-WS客戶端使用ISO-8859-1而不是UTF-8?

[英]Howto setup JAX-WS client to use ISO-8859-1 instead of UTF-8?

我想配置我的JAX-WS客戶端以ISO-8859-1發送消息。 目前使用UTF-8

以下是客戶端嘗試執行的操作:

Map<String, Object> reqContext = ((BindingProvider) service).getRequestContext();
Map httpHeaders = new HashMap();
httpHeaders.put("Content-type",Collections.singletonList("text/xml;charset=ISO-8859-1"));
reqContext.put(MessageContext.HTTP_REQUEST_HEADERS, httpHeaders);

但是此設置被忽略,tcpmon顯示服務器收到以下內容:

POST /service/helloWorld?WSDL HTTP/1.1
Content-type: text/xml;charset="utf-8"
Soapaction: "helloWorld"
Accept: text/xml, multipart/related, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
User-Agent: Oracle JAX-WS 2.1.5
Host: 1.1.1.1:8001
Connection: keep-alive
Content-Length: 4135

<?xml version='1.0' encoding='UTF-8'?>
<S:Envelopexmlns:S="http://schemas.xmlsoap.org/soap/envelope/">...  

因此,設置被覆蓋,並且在HTTP頭和XML消息中都使用了UTF-8。 該服務由WSDL定義,WSDL以UTF-8編碼。

問:我應該重新定義要在ISO-8899-1中編碼的服務WSDL,然后重新生成客戶端嗎? 或者,是不是我沒有正確設置HTTP標頭?

使用處理程序

public class MyMessageHandler implements SOAPHandler<SOAPMessageContext> {

@Override
public boolean handleMessage(SOAPMessageContext context) {
    Boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    if (outbound.booleanValue()) {
        try {
            context.getMessage().setProperty(SOAPMessage.CHARACTER_SET_ENCODING,
                            "ISO-8859-1");
        }
        catch (SOAPException e) {
            throw new RuntimeException(e);
        }
    }
    return true;
}

並注冊處理程序:

    BindingProvider bindProv = (BindingProvider) service;
    List<Handler> handlerChain = bindProv.getBinding().getHandlerChain();
    handlerChain.add(new MyMessageHandler ());

jaypi的答案似乎是正確的。 但我需要添加一些默認實現。 內聯也很容易:

更新:我想你必須明確設置handlerChain。 更改getHandlerChain的結果將不起作用。

    List<Handler> chain = bindingProvider.getBinding().getHandlerChain();
    chain.add(new SOAPHandler<SOAPMessageContext>() {
      @Override
      public boolean handleMessage(SOAPMessageContext context) {
        LOG.info("BaseService.handleMessage" + context);
        Boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        if (outbound.booleanValue()) {
          try {
            context.getMessage().setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "ISO-8859-1");
          } catch (Exception e) {
            throw new RuntimeException(e);
          }
        }
        return true;        
      }

      @Override
      public boolean handleFault(SOAPMessageContext context) {
        return true;
      }

      @Override
      public void close(MessageContext context) {
      }

      @Override
      public Set<QName> getHeaders() {
        return null;
      }      
    });
    bindingProvider.getBinding().setHandlerChain(chain);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM