2015-06-18 53 views
2

我配置了一個入站HTTP網關,它接受POST請求(JSON)並完成作業以返回JSON響應,請求和響應負載是相同的POJO。Spring集成中的JSON映射錯誤

我創建JSON的轉換豆如下

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

然後我就打電報通知他們最多在同一個Java配置類門的方式定義,摘要如下:

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

而且POJO爲此我打算變換非常直接

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

然而, POST請求與Content-Type頭設置爲application/JSON返回400,我發送JSON是

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

我得到的迴應

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

,並在日誌

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: [email protected]; 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: [email protected]; 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: [email protected]; 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: [email protected]; line: 1, column: 1] 
+0

我錯過了這個'gateway.setRequestPayloadType(ApplianceStatus.class);' –

回答

1

的問題是由於請求的有效載荷類型未設置在網關上

gateway.setRequestPayloadType(ApplianceStatus.class); 

解決了它。