在Spring Integration Java DSL中设置响应Http:InboundGateway状态码
下面的配置接受一个HTTP POST,其中包含一个要创建的用户实例的JSON请求体,但是如果我能让它返回一个在Spring Integration Java DSL中设置响应Http:InboundGateway状态码,java,spring-integration,spring-integration-dsl,spring-integration-http,Java,Spring Integration,Spring Integration Dsl,Spring Integration Http,下面的配置接受一个HTTP POST,其中包含一个要创建的用户实例的JSON请求体,但是如果我能让它返回一个201 Created,我会感到不安。有什么想法吗 @Bean public IntegrationFlow flow(UserService userService) { return IntegrationFlows.from( Http.inboundGateway("/users") .requestMapping(r -&g
201 Created
,我会感到不安。有什么想法吗
@Bean
public IntegrationFlow flow(UserService userService) {
return IntegrationFlows.from(
Http.inboundGateway("/users")
.requestMapping(r -> r.methods(HttpMethod.POST))
.statusCodeFunction(f -> HttpStatus.CREATED)
.requestPayloadType(User.class)
.replyChannel(replyChannel())
.requestChannel(inputChannel())
)
.handle((p, h) -> userService.create((User) p)).get();
}
我试着在
HttpRequestHandlerEndpointSpec
上调用statusCodeFunction
,但我一定是做错了。答案是statusCodeFunction
只对入站适配器起作用(即单向进入)。我有点想问,为什么我可以在网关上调用它,但呵呵
在IntegrationFlow
上使用enrichHeaders
就成功了
@Configuration
@EnableIntegration
@Profile("integration")
public class IntegrationConfiguration {
@Autowired
UserService userService;
@Bean
public DirectChannel inputChannel() {
return new DirectChannel();
}
@Bean
public DirectChannel replyChannel() {
return new DirectChannel();
}
@Bean
public HttpRequestHandlingMessagingGateway httpGate() {
HttpRequestHandlingMessagingGateway gateway = new HttpRequestHandlingMessagingGateway(true);
RequestMapping requestMapping = new RequestMapping();
requestMapping.setMethods(HttpMethod.POST);
requestMapping.setPathPatterns("/users");
gateway.setRequestPayloadType(User.class);
gateway.setRequestMapping(requestMapping);
gateway.setRequestChannel(inputChannel());
gateway.setReplyChannel(replyChannel());
return gateway;
}
@Bean
public IntegrationFlow flow(UserService userService) {
return IntegrationFlows.from(httpGate()).handle((p, h) -> userService.create((User) p))
.enrichHeaders(
c -> c.header(org.springframework.integration.http.HttpHeaders.STATUS_CODE, HttpStatus.CREATED))
.get();
}
}
啊!!抢手货是的,事实上,状态代码与回复完全相关。就像我们所做的
ResponseEntity
及其状态一样,这里同样适用于回复。因此,是的,必须设置HttpHeaders.STATUS\u code
头。我有完全相同的代码,但仍然收到“超时内未收到回复”。我只是将Http.inboundGateway替换为Http.inboundChannelAdapter来修复它。有人能解释一下为什么会以http状态响应,以及我何时会通过inboundChannelAdapter使用/不使用inboundGateway吗?