我想配置我的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消息中,使用了设置Overriden,并且使用UTF-8。该服务由在UTF-8中编码的WSDL定义。
Q:是否应该重新定义要在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的答案似乎是正确的。但是我需要添加一些默认实现。也很容易将内联放置:
更新:我想您必须明确设置处理链。更改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);