JAX-WS 파견 응답에서 잘못된 인코딩
-
03-07-2019 - |
문제
Jax-Ws와 함께 웹 서비스에 액세스하려고합니다.
Dispatch<Source> sourceDispatch = null;
sourceDispatch = service.createDispatch(portQName, Source.class, Service.Mode.PAYLOAD);
Source result = sourceDispatch.invoke(new StreamSource(new StringReader(req)));
System.out.println(sourceToXMLString(result));
어디:
private static String sourceToXMLString(Source result)
throws TransformerConfigurationException, TransformerException {
String xmlResult = null;
try {
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
OutputStream out = new ByteArrayOutputStream();
StreamResult streamResult = new StreamResult();
streamResult.setOutputStream(out);
transformer.transform(result, streamResult);
xmlResult = streamResult.getOutputStream().toString();
} catch (TransformerException e) {
e.printStackTrace();
}
return xmlResult;
}
UTF-8 페이지에서 결과를 인쇄하면 UTF-8 문자가 올바르게 표시되지 않습니다.
WS는 다른 도구 (UTF-8을 반환)에서 잘 작동하기 때문에 내 변형에 문제가 있다고 생각하는 경향이 있습니다. sourcetoxmlstring (). 이것이 내 인코딩을 파괴 할 수 있습니까?
해결책
다음을 시도하십시오.
private static String sourceToXMLString(Source result) throws TransformerConfigurationException, TransformerException {
String xmlResult = null;
try {
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
ByteArrayOutputStream out = new ByteArrayOutputStream();
transformer.transform(result, new StreamResult(out));
xmlResult = out.toString("UTF-8");
// or xmlResult = new String(out.toByteArray(), "UTF-8");
} catch (TransformerException e) {
e.printStackTrace();
}
return xmlResult;
}
제휴하지 않습니다 StackOverflow