【问题标题】:<int-http:outbound-gateway > how to use message-converters Attributes in Spring Integration<int-http:outbound-gateway> Spring 集成中如何使用 message-converters 属性
【发布时间】:2017-10-30 21:11:24
【问题描述】:

我的 Spring 集成配置文件有

<int-http:outbound-gateway request-channel="loadToFtpChannel"
          url="http://localhost:8107/ftpService/processFTP"
          http-method="POST" message-converters="ftpMessgaeConverter"
          expected-response-type="java.lang.String">
    </int-http:outbound-gateway>
    <bean id="ftpMessgaeConverter" class="com.ftp.FTPMessgaeConverter" ></bean>

FTPMessageConverter

public class FTPMessgaeConverter implements HttpMessageConverter<JSONObject> {

    private static final List<MediaType> MEDIA_TYPES = new ArrayList<MediaType>();

    static {
        MEDIA_TYPES.add(MediaType.parseMediaType("application/json"));
    }


    @Override
    public boolean canRead(Class<?> clazz, MediaType mediaType) {
        // TODO Auto-generated method stub
        return String.class.equals(clazz);
    }

    @Override
    public boolean canWrite(Class<?> clazz, MediaType mediaType) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public List<MediaType> getSupportedMediaTypes() {
        // TODO Auto-generated method stub
        return MEDIA_TYPES;
    }
    @Override
    public JSONObject read(Class<? extends JSONObject> clazz, HttpInputMessage inputMessage)
            throws IOException, HttpMessageNotReadableException {
        JSONObject body = null;
        try {
            body = new JSONObject(inputMessage.getBody().toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return body;
    }

    @Override
    public void write(JSONObject t, MediaType contentType, HttpOutputMessage outputMessage)
            throws IOException, HttpMessageNotWritableException {

        System.out.println("outputMessage " + outputMessage.getBody());
        System.out.println("JSONObject " + t);
        System.out.println("contentType " + contentType);
    }

它抛出错误

org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [org.json.JSONObject] and content type [application/json]

request-channel="loadToFtpChannel" 中,我收到 Json 格式的消息,使用此消息我必须为
&lt;int-http:inbound-gateway&gt; 准备源和目标,并且一旦入站网关处理请求,它会将响应发送回我必须通过message-converters="ftpMessgaeConverter" 阅读消息的出站网关。 任何人都可以帮助解决这个问题。谢谢

【问题讨论】:

    标签: spring-integration


    【解决方案1】:

    但你肯定在FTPMessgaeConverter中有这段代码:

    @Override
    public boolean canWrite(Class<?> clazz, MediaType mediaType) {
        // TODO Auto-generated method stub
        return false;
    }
    

    也就是说这个HttpMessageConverter根本不适合发送请求。

    不确定您想要实现什么,但这就是为什么您的转换器未被选中的答案。而且由于您明确使用它 (message-converters="ftpMessgaeConverter"),因此无需考虑任何其他转换器。您可以选择将MappingJackson2HttpMessageConverter 与您的自定义一起使用,并将&lt;util:list&gt; 用于message-converters

    【讨论】:

    • 停止向我发送垃圾邮件。您可以使用更多信息编辑您的问题。请使用正确的格式。否则 cmets 中的代码不可读。尊重我帮助你的时间。
    • 谢谢,已选择转换,并且工作正常。
    猜你喜欢
    • 2015-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    相关资源
    最近更新 更多