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

Ale*_*leš 10 java encoding web-services jax-ws http-headers

我想配置我的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);
Run Code Online (Sandbox Code Playgroud)

但是此设置被忽略,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/">...  
Run Code Online (Sandbox Code Playgroud)

因此,设置被覆盖,并且在HTTP头和XML消息中都使用了UTF-8.该服务由WSDL定义,WSDL以UTF-8编码.

问:我应该重新定义要在ISO-8899-1中编码的服务WSDL,然后重新生成客户端吗?或者,是不是我没有正确设置HTTP标头?

小智 8

使用处理程序

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;
}
Run Code Online (Sandbox Code Playgroud)

并注册处理程序:

    BindingProvider bindProv = (BindingProvider) service;
    List<Handler> handlerChain = bindProv.getBinding().getHandlerChain();
    handlerChain.add(new MyMessageHandler ());
Run Code Online (Sandbox Code Playgroud)