Java json响应中未转义Webflux MethodNotAllowedException
我在2.3.1.0版本中使用webflux 我已经映射了端点Java json响应中未转义Webflux MethodNotAllowedException,java,spring,spring-boot,spring-webflux,Java,Spring,Spring Boot,Spring Webflux,我在2.3.1.0版本中使用webflux 我已经映射了端点 @PostMapping(value = "/do-something", produces = MediaType.APPLICATION_JSON_VALUE) public Mono<ResponseEntity<Void>> doSomething( @Valid @RequestBody final Mono<DoSomethingCommand> comman
@PostMapping(value = "/do-something", produces = MediaType.APPLICATION_JSON_VALUE)
public Mono<ResponseEntity<Void>> doSomething(
@Valid @RequestBody final Mono<DoSomethingCommand> commandMono
)
在响应头中,我可以看到内容类型:application/json
,但它不是json
我需要做什么才能使默认错误处理程序返回正确的json?我建议为所有提供有效json的errormessages添加自定义错误处理程序:
@Component
@Order(-2)
public class GlobalErrorWebExceptionHandler extends
AbstractErrorWebExceptionHandler {
// constructors
@Override
protected RouterFunction<ServerResponse> getRoutingFunction(
ErrorAttributes errorAttributes) {
return RouterFunctions.route(
RequestPredicates.all(), this::renderErrorResponse);
}
private Mono<ServerResponse> renderErrorResponse(
ServerRequest request) {
Map<String, Object> errorPropertiesMap = getErrorAttributes(request,
ErrorAttributeOptions.defaults());
return ServerResponse.status(HttpStatus.BAD_REQUEST)
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(errorPropertiesMap));
}
}
@组件
@订单(-2)
公共类GlobalErrorWebExceptionHandler扩展
AbstractErrorWebExceptionHandler{
//建设者
@凌驾
受保护的路由函数getRoutingFunction(
错误属性(错误属性){
返回路由函数(
RequestPredicates.all(),this::renderErrorResponse);
}
专用单声道渲染器响应(
服务器请求(请求){
映射errorPropertiesMap=getErrorAttributes(请求,
ErrorAttributeOptions.defaults());
返回ServerResponse.status(HttpStatus.BAD\u请求)
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(errorPropertiesMap));
}
}
我不知道您是否可以更改
DefaultErrorWebExceptionHandler
无需覆盖全局异常处理程序,因为他使用的是基于注释的端点标准@ExceptionHandler
已经足够了
@Component
@Order(-2)
public class GlobalErrorWebExceptionHandler extends
AbstractErrorWebExceptionHandler {
// constructors
@Override
protected RouterFunction<ServerResponse> getRoutingFunction(
ErrorAttributes errorAttributes) {
return RouterFunctions.route(
RequestPredicates.all(), this::renderErrorResponse);
}
private Mono<ServerResponse> renderErrorResponse(
ServerRequest request) {
Map<String, Object> errorPropertiesMap = getErrorAttributes(request,
ErrorAttributeOptions.defaults());
return ServerResponse.status(HttpStatus.BAD_REQUEST)
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromValue(errorPropertiesMap));
}
}