Spring Integration webflux - проверить входное тело JSON - PullRequest
2 голосов
/ 24 июня 2019

Ниже приведена конфигурация моего приложения

@SpringBootApplication
class DemoApplication {

    static void main(String[] args) {
        SpringApplication.run(DemoApplication, args)
    }


    @Bean
    IntegrationFlow startHeatToJiraFlow() {
        IntegrationFlows
                .from(WebFlux.inboundGateway("/input1")
                .requestMapping { m -> m.methods(HttpMethod.POST).consumes(MediaType.APPLICATION_JSON_VALUE) }
                .requestPayloadType(ResolvableType.forClassWithGenerics(Mono, ServiceInput))
        )
                .channel("inputtestchannel")
                .get()
    }

    @ServiceActivator(inputChannel = "inputtestchannel")
    Map replyMessage() {
        return [success: true]
    }


    class ServiceInput {
        @NotBlank
        String input1
        @NotBlank
        String input2
    }
}

Я ожидаю, что следующий запрос curl выдаст мне ошибку, так как я не даю ввод JSON в теле.

curl  -X POST localhost:8080/input1 -H "Content-Type:application/json"

Вместо этого я получаю ответ 200

{"success":true}

Что я здесь не так делаю?

1 Ответ

2 голосов
/ 24 июня 2019

WebFlux DSL не поддерживает проверку. Вы можете проверить ответ как часть вашей последовательности обработки, как описано в разделе проверки документа webflux .

Пример подключения его в Spring Integration может выглядеть примерно так:

@EnableIntegration
@Configuration
class ValidatingFlowConfiguration {

  @Autowired
  Validator validator

  @Bean
  Publisher<Message<String>> helloFlow() {
    IntegrationFlows
        .from(
            WebFlux
                .inboundGateway("/greet")
                .requestMapping { m ->
                  m
                      .methods(HttpMethod.POST)
                      .consumes(MediaType.APPLICATION_JSON_VALUE)
                }
                .requestPayloadType(ResolvableType.forClassWithGenerics(Flux, HelloRequest))
                .requestChannel(greetingInputChannel())
        )
        .toReactivePublisher()
  }

  @Bean
  MessageChannel greetingInputChannel() {
    return new FluxMessageChannel()
  }

  @ServiceActivator(
      inputChannel = "greetingInputChannel"
  )
  Flux<String> greetingHandler(Flux<HelloRequest> seq) {
    seq
        .doOnNext { HelloRequest it -> validate(it) }
        .log()
        .map { "Hello, ${it.name}" as String }
  }

  void validate(HelloRequest request) {
    Errors errors = new BeanPropertyBindingResult(request, "request")
    validator.validate(request, errors);
    if (errors.hasErrors()) {
      throw new ServerWebInputException(errors.toString());
    }
  }
}

@ToString(includeNames = true)
@Validated
class HelloRequest {

  @NotEmpty
  String name
}

См. gist , если вы хотите импортировать.

...