1
votes

My Spring integration configuration file having

<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>

and FTPMessageConverter class

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);
    }

its throwing the error

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

In request-channel="loadToFtpChannel" I am getting the message in Json format, with this message I have to prepare the source and destination for
<int-http:inbound-gateway> and once inbound-gateway process the request, it will send the response back to outbound-gateway where I have to read the message through message-converters="ftpMessgaeConverter". Can any one help on this. Thanks

1

1 Answers

0
votes

But you definitely have this code in the FTPMessgaeConverter:

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

That says that this HttpMessageConverter isn't suitable for sending requests at all.

Not sure what you want to achieve, but this is an answer why your converter isn't selected. And since you use it explicitly (message-converters="ftpMessgaeConverter"), there is no any other converters to consider. You may choose to use MappingJackson2HttpMessageConverter alongside with your custom and a <util:list> for the message-converters though.