gpt4 book ai didi

java - 设置响应 Http :InboundGateway StatusCode in Spring Integration Java DSL

转载 作者:行者123 更新时间:2023-11-30 06:28:53 26 4
gpt4 key购买 nike

以下配置接受带有要创建的 User 实例的 JSON 请求正文的 HTTP POST,但如果我能让它返回 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();
}
}

关于java - 设置响应 Http :InboundGateway StatusCode in Spring Integration Java DSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46532167/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com