2

I have configured an inbound HTTP gateway, which takes POST requests (JSON) and does homework to return a JSON response, the request and response payload is the same POJO.

I created beans for Json converters as follows

    @Bean
    public Jackson2ObjectMapperBuilder jacksonBuilder() {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        builder.indentOutput(true);
        return builder;
    }

    @Bean
    public List<HttpMessageConverter<?>> getConverters(){

        List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
        converters.add(new MappingJackson2HttpMessageConverter(jacksonBuilder().build()));
        return converters;
    }

And then I wired them up to the gate way definition in same Java Config class, snippet as follows:

@Bean
    public HttpRequestHandlingMessagingGateway gateway(){

        HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
        RequestMapping requestMapping = new RequestMapping();
        requestMapping.setMethods(HttpMethod.POST);
        requestMapping.setPathPatterns("/appliance/v1/status");
        requestMapping.setConsumes("application/json");
        requestMapping.setProduces("application/json");
        gateway.setRequestMapping(requestMapping);
        gateway.setRequestChannel(requestChannel());
        gateway.setReplyChannel(replyChannel());
        gateway.setMessageConverters(getConverters());
        return gateway;
    }

And the POJO for which I intend transform is pretty straight forward

public class ApplianceStatus {

    private String gatewayId;

    private String applianceId;

    private boolean running;

    public String getGatewayId() {
        return gatewayId;
    }

    public void setGatewayId(String gatewayId) {
        this.gatewayId = gatewayId;
    }

    public String getApplianceId() {
        return applianceId;
    }

    public void setApplianceId(String applianceId) {
        this.applianceId = applianceId;
    }

    public boolean isRunning() {
        return running;
    }

    public void setRunning(boolean running) {
        this.running = running;
    }
}

However a POST Request to with Content-Type header set to application/json returns 400, the JSON I send is

{
    "gatewayId": 1,
    "applianceId": 123,
    "running": false
}

I get the response

{
  "timestamp" : 1434615561240,
  "status" : 400,
  "error" : "Bad Request",
  "exception" : "org.springframework.http.converter.HttpMessageNotReadableException",
  "message" : "Bad Request",
  "path" : "/appliance/v1/status"
}

and In logs

2015-06-18 14:55:30.501 DEBUG 3447 --- [tp1023996917-22] .w.s.m.a.ResponseStatusExceptionResolver : Resolving exception from handler [gateway]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of byte[] out of START_OBJECT token
 at [Source: HttpInputOverHTTP@55c2d2c5; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of byte[] out of START_OBJECT token
 at [Source: HttpInputOverHTTP@55c2d2c5; line: 1, column: 1]
2015-06-18 14:55:30.501 DEBUG 3447 --- [tp1023996917-22] .w.s.m.s.DefaultHandlerExceptionResolver : Resolving exception from handler [gateway]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of byte[] out of START_OBJECT token
 at [Source: HttpInputOverHTTP@55c2d2c5; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of byte[] out of START_OBJECT token
 at [Source: HttpInputOverHTTP@55c2d2c5; line: 1, column: 1]
Anadi Misra
  • 1,925
  • 4
  • 39
  • 68

1 Answers1

1

The problem was due to request payload type not set on gateway

gateway.setRequestPayloadType(ApplianceStatus.class);

That solved it.

Anadi Misra
  • 1,925
  • 4
  • 39
  • 68