以下config接受了用户实例的JSON请求主体来创建的HTTP帖子,但是如果可以让它返回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
仅适用于入站适配器(即在途中单向的东西)。Kinda乞求我为什么可以在A 网关上称呼它的问题,但是ho-hum ...
在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();
}
}