以下是我的应用程序配置
@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}
我在这里做错了什么?
最佳答案
WebFlux DSL 不支持验证。您可以验证响应作为处理序列的一部分,如 validation section of the webflux docs 中所述。 .
将其插入 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如果您想要进口。
关于groovy - Spring Integration webflux - 验证输入 JSON 正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56729517/