Spring Boot-Jaxb2Marshaller仍然转换特殊字符

时间:2018-09-23 18:00:50

标签: spring soap escaping character

我正在尝试调用基于肥皂的Web服务。 Jaxb2Marshaller将其编组之前的请求是预期的。但是,当涉及到调用时,它会以转义符发送,并且目标服务器会以无法解析请求的消息进行回复。 我还尝试设置Marshaller.properties(com.sun.xml.bind.characterEscapeHandler),但是我收到了Jaxb2Excpetion,该给定的属性无法识别。

我的代码如下:

public class Client extends WebServiceGatewaySupport {

private static final Logger log = LoggerFactory.getLogger(VotingClient.class);

@SuppressWarnings("unchecked")
public GetSmsINResponse test() throws Exception {
    GetSmsIN request = new GetSmsIN();
    request.setUserName("user");
    request.setUserPass("pass");
    Xmldoc xml = new Xmldoc();
    xml.getContent().add("<Packet>");
    xml.getContent().add("<SMS>");
    xml.getContent().add("<SpID>");
    xml.getContent().add("D01");
    xml.getContent().add("</SpID>");
    xml.getContent().add("<Msgtxt>");
    xml.getContent().add("text 3");
    xml.getContent().add("</Msgtxt>");
    xml.getContent().add("<VotingCount>");
    xml.getContent().add("3");
    xml.getContent().add("</VotingCount>");
    xml.getContent().add("</SMS>");
    xml.getContent().add("</Packet>");

    request.setXmldoc(xml);
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();     
    Map<String,Object> map = new HashMap<String,Object>();
    //map.put(Marshaller.JAXB_ENCODING , "US-ASCII");
    CharacterEscapeHandler escapeHandler = new JaxbCharacterEscapeHandler();
    map.put("com.sun.xml.bind.characterEscapeHandler", escapeHandler);
    marshaller.setMarshallerProperties(map);    
    marshaller.setContextPath("com.domain");
    marshaller.afterPropertiesSet();

    getWebServiceTemplate().setMarshaller(marshaller);
    getWebServiceTemplate().setUnmarshaller(marshaller);
    getWebServiceTemplate().afterPropertiesSet();
    ClientInterceptor[] interceptors = getWebServiceTemplate().getInterceptors();

    interceptors = (ClientInterceptor[]) ArrayUtils.add(interceptors, new ClientInterceptor() {
        @Override
        public boolean handleRequest(MessageContext messageContext) throws WebServiceClientException {
            return true;
        }

        @Override
        public boolean handleResponse(MessageContext messageContext) throws WebServiceClientException {
            return true;
        }

        @Override
        public boolean handleFault(MessageContext messageContext) throws WebServiceClientException {
            return true;
        }

        @Override
        public void afterCompletion(MessageContext messageContext, Exception ex) throws WebServiceClientException {
            try {
                System.out.println("Request :");
                messageContext.getRequest().writeTo(System.out);
                System.out.println("\nResponse : ");
                messageContext.getResponse().writeTo(System.out);
                System.out.println();
            } catch (IOException ignored) {
            }
        }
    });

    getWebServiceTemplate().setInterceptors(interceptors);

    GetSmsINResponse response = (GetSmsINResponse) getWebServiceTemplate().marshalSendAndReceive(
            "url", request,
            new SoapActionCallback("url"));

    return response;
}

class JaxbCharacterEscapeHandler implements CharacterEscapeHandler {
    public void escape(char[] buf, int start, int len, boolean isAttValue, Writer out) throws IOException {
        for (int i = start; i < start + len; i++) {
            char ch = buf[i];
            out.write(ch);
        }
    }
}

}

XmlClass如下:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "userName",
    "userPass",
    "xmldoc"
})
@XmlRootElement(name = "GetSmsIN")

public class GetSmsIN {

    @XmlElement(name = "UserName")
    protected String userName;
    @XmlElement(name = "UserPass")
    protected String userPass;
    protected GetSmsIN.Xmldoc xmldoc;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String value) {
        this.userName = value;
    }
    public String getUserPass() {
        return userPass;
    }
    public void setUserPass(String value) {
        this.userPass = value;
    }

    public GetSmsIN.Xmldoc getXmldoc() {
        return xmldoc;
    }
    public void setXmldoc(GetSmsIN.Xmldoc value) {
        this.xmldoc = value;
    }
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "content"
    })
    public static class Xmldoc {

        @XmlMixed
        @XmlAnyElement(lax = true)
        protected List<Object> content;

        public List<Object> getContent() {
            if (content == null) {
                content = new ArrayList<Object>();
            }
            return this.content;
        }

    }

}

谢谢。

0 个答案:

没有答案