Estoy tratando de implementar mi clase personalizada GlobalExceptionHandler extendiendo AbstractErrorWebExceptionHandler (la implementación predeterminada es la clase DefaultErrorWebExceptionHandler) pero no puedo hacerlo porque falta el bean (indicado a continuación), que es necesario en la inicialización del constructor. No estoy seguro de por qué sucede esto de forma predeterminada la implementación está funcionando bien y al dar mi propia implementación está pidiendo un bean, por favor ayuda
@Component @Order(-2) public class GlobalExceptionHandler extends AbstractErrorWebExceptionHandler{ public GlobalExceptionHandler(ErrorAttributes errorAttributes, Resources resources, ApplicationContext applicationContext) { super(errorAttributes, resources, applicationContext); } @Override protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) { return RouterFunctions.route(RequestPredicates.all(),this::formatErrorResponse); } private Mono<ServerResponse> formatErrorResponse(ServerRequest request){ Map<String, Object> errorAttributesMap = getErrorAttributes(request, ErrorAttributeOptions.defaults()); int status = (int) Optional.ofNullable(errorAttributesMap.get("status")).orElse(500); return ServerResponse .status(status) .contentType(MediaType.APPLICATION_JSON) .body(BodyInserters.fromValue(errorAttributesMap)); } }Y el error que estoy recibiendo es:
*************************** APPLICATION FAILED TO START *************************** Description: Parameter 1 of constructor in com.example.userManagementSystem.demoApp.exception.GlobalExceptionHandler required a bean of type 'org.springframework.boot.autoconfigure.web.WebProperties$Resources' that could not be found. Action: Consider defining a bean of type 'org.springframework.boot.autoconfigure.web.WebProperties$Resources' in your configuration. Process finished with exit code 1No estoy seguro de por qué viene esto. ¡Por favor ayuda!
@Slf4j @Component @Order(-99) public class ExceptionHandler implements WebExceptionHandler { @Override public Mono<Void> handle(ServerWebExchange serverWebExchange, Throwable throwable) { ServerHttpResponse response = serverWebExchange.getResponse(); response.setStatusCode(HttpStatus.BAD_REQUEST); response.getHeaders().setContentType(MediaType.APPLICATION_JSON); JSONObject resMsg = new JSONObject(); try { resMsg.put("code", HttpStatus.BAD_REQUEST.value()); if(throwable instanceof CommonException){ resMsg.put("msg", ((CommonException) throwable).getMsg()); }else{ log.error("system error:", throwable); resMsg.put("msg", CommonCode.PLATFORM_ERR_MSG); } } catch (Exception e) { } DataBuffer db = response.bufferFactory().wrap(resMsg.toString().getBytes(Charset.forName("UTF-8"))); return response.writeWith(Mono.just(db)); } }hermano. Lo siento por mi ingles. Y puede inyectar WebProperties. El siguiente paso puede hacer getResources() desde estas propiedades. Espero que te ayude.
Obtuve la misma excepción cuando migré mi aplicación Webflux api de Springboot versión 2.5.x a 2.6.2.
Para resolverlo, agregué una clase de configuración que crea un Bean para WebProperties.Resources como se muestra a continuación,
@Configuration public class ResourceWebPropertiesConfig { @Bean public WebProperties.Resources resources() { return new WebProperties.Resources(); } }Esto resolvió el problema. Supongo que algo ha cambiado en Springboot versión 2.6.x
La raíz del problema es que ResourceProperties (que es el que estaba vinculado a spring.resources) se eliminó en Spring Boot 2.6 (había quedado obsoleto desde 2.4) como se explica aquí y también en las notas de lanzamiento de dicha versión.
Inyectar el bean WebProperties en su constructor y luego llamar a WebProperties.getResources() en el punto de uso debería corregir su controlador de excepción global personalizado como se muestra en el siguiente fragmento:
@Component @Order(-2) public class GlobalExceptionHandler extends AbstractErrorWebExceptionHandler{ // Spring boot 2.5.x public GlobalExceptionHandler(ErrorAttributes errorAttributes, Resources resources, ApplicationContext applicationContext) { super(errorAttributes, resources, applicationContext); } // Spring boot 2.6.0 public GlobalExceptionHandler(ErrorAttributes errorAttributes, WebProperties webProperties, ApplicationContext applicationContext) { super(errorAttributes, webProperties.getResources(), applicationContext); }