Groovy Spring集成webflux-验证输入JSON正文
下面是我的应用程序配置Groovy Spring集成webflux-验证输入JSON正文,groovy,spring-integration,spring-webflux,project-reactor,Groovy,Spring Integration,Spring Webflux,Project Reactor,下面是我的应用程序配置 @SpringBootApplication class DemoApplication { static void main(String[] args) { SpringApplication.run(DemoApplication, args) } @Bean IntegrationFlow startHeatToJiraFlow() { IntegrationFlows
@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不支持验证。您可以将响应作为处理序列的一部分进行验证,如中所述 将其插入Spring集成的示例如下所示:
@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
}
@使能集成
@配置
类验证流配置{
@自动连线
验证器
@豆子
出版商helloFlow(){
集成流
.来自(
WebFlux
.inboundGateway(“/greet”)
.requestMapping{m->
M
.方法(HttpMethod.POST)
.consumes(MediaType.APPLICATION_JSON_值)
}
.requestPayloadType(ResolvableType.forClassWithGenerics(Flux,HelloRequest))
.requestChannel(greetingInputChannel())
)
.toReactivePublisher()
}
@豆子
MessageChannel greetingInputChannel(){
返回新的FluxMessageChannel()
}
@服务激活器(
inputChannel=“问候inputChannel”
)
通量迎宾处理器(通量顺序){
序号
.doOnNext{HelloRequest it->validate(it)}
.log()
.map{“Hello,${it.name}”作为字符串}
}
无效验证(HelloRequest请求){
Errors Errors=newbeanPropertyBindingResult(请求,“请求”)
验证(请求、错误);
if(errors.hasErrors()){
抛出新的ServerWebInputException(errors.toString());
}
}
}
@ToString(includeNames=true)
@验证
类HelloRequest{
@空空如也
字符串名
}
看看你是否需要进口。我想你的
doOnNext()
验证不会被调用,因为记者说他没有发送任何东西。因此,Flux
是空的,因此我们直接进入complete()
,而不进行任何处理。这就是我们如何得到没有任何错误的200 OK
。我需要更多地了解WebFlux中的这种验证技巧,我对Spring集成在这方面的改进持完全开放的态度。WebFlux手册中描述了这种技巧(请参阅答案中的第一个链接),它是有效的:```❯ http POST foo=bar http/1.1 400错误请求内容类型:application/json;charset=UTF-8{“错误”:“错误请求”,“消息”:“org.springframework.validation.BeanPropertyBindingResult:1错误\n字段“名称”上的对象“请求”中的字段错误:拒绝值[null];…”,“路径”:“/问候”,“状态”:400,“时间戳”:“2019-06-24T23:14:04.150+0000”,“跟踪”:“。”}“``如果你想玩,这里有一个完整的项目:谢谢分享这个示例!我以后再看。如果您不发送foo=bar
,它是如何工作的?请参阅此处了解Spring Integration中的开箱即用解决方案: