2017-06-27 54 views
1

我有一個系統使用spring集成來交換數據。 AppClient請求帶有TableInfoRequest的AppServer,並且AppServer將響應TableInfoResponse.It現在工作正常。如何在Spring集成中響應HTTP 204

  

    @MessagingGateway() 
    public interface SyncGateway { 

     @Gateway(requestChannel = "requestFlow.input") 
     TableInfoResponse info(TableInfoRequest payload); 

    } 

但我想用HTTP代碼204迴應時,沒有數據來響應,這將降低相關的數據。那麼我怎樣才能設置DSL?

 

    @Bean 
     public IntegrationFlow syncFlow() { 
     return IntegrationFlows 
      .from(
       Http.inboundGateway(HTTP_ENDPOINT_PATH).id("syncGateway") 
       .replyTimeout(serverProps.getReplyTimeout() * 1000) 
       .requestTimeout(serverProps.getRequestTimeout() * 1000) 
       .messageConverters(new SerializingHttpMessageConverter()) 
       .convertExceptions(true) 
       .mappedResponseHeaders(IDENTIFICATION, TOKEN)) 
      .channel(c -> c.direct("syncChannel") 
       .interceptor(new ChannelAuthenticationInterceptor(dataService))) 
      .handle(syncHandler) 
      .get(); 
     } 

的syncHandler將返回TableInfoResponse.And AppClient使用 syncGateway.info(要求)得到tableInfoResponse。

回答

0

在回覆消息中設置HttpHeaders.STATUS_CODE標頭(http_statusCode)。

+0

換句話說,如果你想返回'204 No Content'回覆,你應該這麼做 - 用'HttpStatus.NO_CONTENT'回覆Inbond網關:http://docs.spring.io/spring-integration /reference/html/http.html#http-response-statuscode –

+0

感謝您的回答。但我不明白如何在沒有數據時回覆204。 '.handle(syncHandler)'也會回覆一個對象,並且AppClient也希望接收一個對象 - '.expectedResponseType(Serializable.class)' – Roy