<int-http:outbound-gateway>如何在Spring Integration中使用消息转换器属性

时间:2017-10-30 11:36:19

标签: spring-integration

My 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格式的消息,有了这条消息,我必须为源和目的地做准备  <int-http:inbound-gateway>并且一旦入站网关处理请求,它就会将响应发送回出站网关,我必须通过message-converters="ftpMessgaeConverter"读取消息。 任何人都可以为此提供帮助。感谢

1 个答案:

答案 0 :(得分:0)

但你肯定在FTPMessgaeConverter

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

这表示此HttpMessageConverter根本不适合发送请求。

不确定您想要达到的目标,但这就是为什么您的转换器未被选中的答案。由于您明确使用它(message-converters="ftpMessgaeConverter"),因此没有其他转换器需要考虑。您可以选择在自定义旁边使用MappingJackson2HttpMessageConverter,在<util:list>使用message-converters