1

以下配置接受带有要创建的用户实例的 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();
}

我尝试调用statusCodeFunctionHttpRequestHandlerEndpointSpec但我一定做错了。

4

1 回答 1

2

答案是statusCodeFunction只适用于入站适配器(即单向进入的东西)。Kinda 提出了一个问题,为什么我可以在网关上调用它,但是哼哼......

使用成功enrichHeadersIntegrationFlow

@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();
    }
}
于 2017-10-02T19:47:21.157 回答