java 如何设置 JAX-WS 客户端以使用 ISO-8859-1 而不是 UTF-8?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13216915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Howto setup JAX-WS client to use ISO-8859-1 instead of UTF-8?
提问by Ale?
I would like to configure my JAX-WS client to send messages in ISO-8859-1. Currently UTF-8is used.
我想配置我的 JAX-WS 客户端以在ISO-8859-1 中发送消息。目前使用UTF-8。
Here is what the client tries to do:
这是客户端尝试执行的操作:
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);
But this setting is ignored and tcpmon shows that the following is received by the server:
但此设置被忽略,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/">...
So, the setting is overriden and UTF-8 is used, both in the HTTP header and in the XML message. The service is defined by the WSDL which is encoded in UTF-8.
因此,该设置将被覆盖并在 HTTP 标头和 XML 消息中使用 UTF-8。该服务由以 UTF-8 编码的 WSDL 定义。
Q:Should I redefine the service WSDL to be encoded in ISO-8899-1 and then regenerate the client? Or, is it that I am just not setting the HTTP headers properly?
问:我是否应该重新定义要在 ISO-8899-1 中编码的服务 WSDL,然后重新生成客户端?或者,是我没有正确设置 HTTP 标头?
采纳答案by jaypi
Using handler:
使用处理程序:
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;
}
And register the handler:
并注册处理程序:
BindingProvider bindProv = (BindingProvider) service;
List<Handler> handlerChain = bindProv.getBinding().getHandlerChain();
handlerChain.add(new MyMessageHandler ());
回答by Nicholas DiPiazza
The answer from jaypi seems right. But I needed to add some default implementations. Also it was easy to put inline:
jaypi 的答案似乎是正确的。但我需要添加一些默认实现。也很容易内联:
UPDATE: I guess you have to set the handlerChain explicitly. changing the result of getHandlerChain will do nothing.
更新:我猜你必须明确设置 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);