http出站网关不转换法语字符

时间:2016-02-09 14:08:42

标签: spring-integration

我有我的spring集成http出站网关就像这样。我正在使用我自己的休息模板与ssl池连接。我有500个法语字符因此我注入了UTF-8和支持的媒体类型到消息转换器。现在我注入我自己的请求工厂和默认休息模板注入后,它开始接受法语字符。现在我改变使用我自己的休息模板它不接受消息转换器,当我尝试注入这个我的休息模板我得到了这个例外

Cause is - Could not write request: no suitable HttpMessageConverter found for request type [java.lang.String] and content type [text/plain;charset=UTF-8]

这适用于所有请求

<int-http:outbound-gateway id='batch-http'  header-mapper="headerMapper"
                           request-channel='toHttp'
                            request-factory="requestFactory"
                             message-converters="batchHTTPConverter"
                             url-expression="payload.contains('${filterAttribute}') ? '${url1}' : '${url2}'"  http-method="${httpMethod}"
                           expected-response-type='java.lang.String' charset='${charset}'
                           reply-timeout='${replyTimeout}' reply-channel='output'>
</int-http:outbound-gateway>

 <beans:bean  id="batchHTTPConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
    <beans:constructor-arg index="0"  value="UTF-8"/>
    <beans:property name="supportedMediaTypes" value = "application/json;UTF-8" />

</beans:bean>


<beans:bean id="requestFactory" class="test.batch.httpclient.CustomClientHttpRequestFactory">       
 <beans:constructor-arg ref="verifier"/>
  </beans:bean>

 <beans:bean id="verifier"
      class="batch.NullHostnameVerifier">

</beans:bean>

这不适用于法语或任何输入请求     

    <header-filter input-channel="input"
                   output-channel="inputX" header-names="x-death"/>

    <service-activator input-channel="inputX" ref="gw" />

    <gateway id="gw" default-request-channel="toHttp" default-reply-timeout="0"  error-channel="errors" />

    <beans:bean id="inputfields" class="testbatch.httpclient.HTTPInputProperties">
        <beans:property name="nonRetryErrorCodes" value="${nonRetryErrorCodes}"/>
    </beans:bean>
    <beans:bean id="responseInterceptor" class="testbatch.httpclient.ResponseInterceptor">
        <beans:property name="inputProperties" ref="inputfields" />
    </beans:bean>

    <chain input-channel="errors" output-channel="output">
        <!-- examine payload.cause (http status code etc) and decide whether
             to throw an exception or return the status code for sending to output -->
        <header-filter header-names="replyChannel, errorChannel" />
        <transformer ref="responseInterceptor"  />
    </chain>


    <int-http:outbound-gateway id='batch-http'  header-mapper="headerMapper"
                               request-channel='toHttp'
                               rest-template="batchRestTemplate"
                               url-expression="payload.contains('${filterAttribute}') ? '${url1}' : '${url2}'"  http-method="${httpMethod}"
                               expected-response-type='java.lang.String' charset='${charset}'
                               reply-timeout='${replyTimeout}' reply-channel='output'>
    </int-http:outbound-gateway>

    <beans:bean  id="batchHTTPConverter" class="org.springframework.http.converter.StringHttpMessageConverter" >
        <beans:constructor-arg index="0"  value="UTF-8"/>
        <beans:property name="supportedMediaTypes" value = "application/json;UTF-8" />

    </beans:bean>




     <beans:bean  id="batchRestTemplate" class="testbatch.httpclient.BatchRestTemplate" >
        <beans:property name="batchHTTPConverter" ref="batchHTTPConverter"/>



</beans:bean>



<beans:bean id="requestFactory"
            class="testbatch.httpclient.CustomClientHttpRequestFactory">

<beans:constructor-arg ref="verifier"/>

</beans:bean>

<beans:bean id="verifier"
            class="testbatch.httpclient.NullHostnameVerifier">

</beans:bean>

<beans:bean id="headerMapper" class="org.springframework.integration.http.support.DefaultHttpHeaderMapper"
            factory-method="outboundMapper">
<beans:property name="outboundHeaderNames" value="${mapHeaders}"/>
<beans:property name="userDefinedHeaderPrefix" value=""/>
</beans:bean>

<channel id="output" />
<channel id="input" />
<channel id="inputX" />
<channel id="toHttp" />

        </beans:beans>

我的休息模板

public class BatchRestTemplate  extends RestTemplate{

     private static final Logger LOGGER = LoggerFactory
                .getLogger(BatchRestTemplate.class);
     private StringHttpMessageConverter batchHTTPConverter;



    public StringHttpMessageConverter getBatchHTTPConverter() {
        return batchHTTPConverter;
    }

    public void setBatchHTTPConverter(StringHttpMessageConverter batchHTTPConverter) {
        this.batchHTTPConverter = batchHTTPConverter;
    }

    public BatchRestTemplate()  {
          super(createBatchHttpRequestFactory());

        List<HttpMessageConverter<?>> messageConverters= new ArrayList<HttpMessageConverter<?>>();
        messageConverters.addAll(getMessageConverters());
        messageConverters.add(getBatchHTTPConverter());
        super.setMessageConverters(messageConverters);

    }

    private static ClientHttpRequestFactory createBatchHttpRequestFactory()  {

            CloseableHttpClient httpClient;
            HttpComponentsClientHttpRequestFactory httpRequestFactory;
            final int timeout = 3000;
            SSLConnectionSocketFactory socketFactory;
            try {
                socketFactory = new SSLConnectionSocketFactory(
                        SSLContext.getDefault(),
                        new String[] {"TLSv1"},
                        null,
                        SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

            Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("http", PlainConnectionSocketFactory.getSocketFactory())
                    .register("https", socketFactory)
                    .build();
            PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
            cm.setMaxTotal(700);
            cm.setDefaultMaxPerRoute(300);
            cm.closeExpiredConnections();


            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout)
                    .setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build();



            httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).setConnectionManager(cm).build();


            httpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);

            return httpRequestFactory;

            }

            catch (Exception e) {
                 LOGGER.debug("error exception", e);


    }
            return null; 
    }


}

1 个答案:

答案 0 :(得分:1)

您自己的代码中存在错误:

public BatchRestTemplate()  {
    ..........
    messageConverters.add(getBatchHTTPConverter());
    ..........

}

但是......还没有batchHTTPConverter!它只会在setBatchHTTPConverter()之后出现。

换句话说,你不能使用构造函数中的属性,因为在对象实例化之后会调用setter。