How to control character encoding in Spring integration TCP gateway

夙愿已清 提交于 2019-12-11 12:36:35

问题


I have set up a Spring integration gateway using the documentation and examples that are available online. I'm sending an XML message that is ISO-8859-1 encoded, but the receiving part is telling me that the message comes to them in an encoding different from ISO-8859-1.

My config looks like this:

    <!-- Spring integration begins -->
<beans:bean id="javaDeserializer"
    class="org.springframework.integration.ip.tcp.serializer.ByteArrayRawSerializer" />

<int:gateway id="gw"
    service-interface="com.pawsec.alarmcenter.model.SOSAccessV4.SimpleGateway"
    default-request-channel="input"/>

<int-ip:tcp-connection-factory id="cfClient"
    type="client"
    host="${rapid.alarm.ipaddress}"
    port="${rapid.alarm.port}"
    deserializer="javaDeserializer"
    single-use="true"
    so-timeout="10000"/>

<int:channel id="input" />

<int-ip:tcp-outbound-gateway id="outGateway"
    request-channel="input"
    reply-channel="clientBytes2StringChannel"
    connection-factory="cfClient"
    request-timeout="10000"/>

<int:channel id="clientBytes2StringChannel"/>

<int:object-to-string-transformer id="clientBytes2String"
    input-channel="clientBytes2StringChannel" />

 <!-- Spring integration ends -->

I need to know how i can control the character encoding of the outgoing stream. Also - is there any way i can check the outgoing message myself without having to call the receiving party and ask him to check?

Can anyone help me please?


回答1:


The TcpNetConnection uses TcpMessageMapper for converting message to the byte[] before sending:

Object object = this.getMapper().fromMessage(message);

Where its code looks like (by default, of course):

bytes = ((String) payload).getBytes(this.charset);

Where it is like:

private volatile String charset = "UTF-8";

So, you should provide your mapper with desired charset to convert ISO-8859-1 XML to bytes.

Re.

check the outgoing message myself

You can use some TCP Trace tool, like wire Wireshark to proxy and intercept the traffic.




回答2:


I solved the original issue by configuring the client factory with a mapper for ISO-8859-1 like this:

        @Bean
    public AbstractClientConnectionFactory clientCF() {
        TcpNetClientConnectionFactory clientConnectionFactory = new TcpNetClientConnectionFactory(HOST, PORT);
        TcpMessageMapper tcpMessageMapper = new TcpMessageMapper();
        logger.debug("Setting deserializer");
        clientConnectionFactory.setDeserializer(new ByteArrayRawSerializer());
        tcpMessageMapper.setCharset("ISO-8859-1");
        logger.debug("setting mapper!");
        clientConnectionFactory.setMapper(tcpMessageMapper);
        return clientConnectionFactory;
    }


来源:https://stackoverflow.com/questions/34949964/how-to-control-character-encoding-in-spring-integration-tcp-gateway

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!