Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebProperties$Resources bean missing for custom AbstractErrorWebExceptionHandler

I am trying to implement my custom GlobalExceptionHandler class by extending AbstractErrorWebExceptionHandler (default implementation is DefaultErrorWebExceptionHandler class) but unable to do so as bean(stated below) is missing which is required in constructer initilization .I am not sure why this is happening as by default implemetation it is working fine and by giving my own implementation it is asking for a bean, Please help

@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));
    }
}

And error i am getting is:

***************************
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 1

I am not sure why this is coming. PLease help!

like image 972
Rishabh Singh Avatar asked Mar 21 '26 14:03

Rishabh Singh


2 Answers

I was getting the same exception when I migrated my Webflux api application from Springboot version 2.5.x to 2.6.2.

To resolve it I added a configuration class which creates a Bean for WebProperties.Resources as below,

@Configuration
public class ResourceWebPropertiesConfig {

    @Bean
    public WebProperties.Resources resources() {
        return new WebProperties.Resources();
    }

}

This solved the issue. I guess something have changed in Springboot version 2.6.x

like image 57
Bala Avatar answered Mar 23 '26 04:03

Bala


The root of the issue is that ResourceProperties (which is the one that was bound to spring.resources) was removed in Spring Boot 2.6 (had been deprecated since 2.4) as explained here and also in the said version release notes.

Injecting the WebProperties bean in your constructor and then calling WebProperties.getResources() at the usage point should fix your custom global exception handler as shown in the following snippet:

@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);
    }
like image 35
Lex Luthor Avatar answered Mar 23 '26 04:03

Lex Luthor