Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.2K
Visualizações
Missing Resources bean : Springboot web-flux Custom Global Exception handler

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!

over 4 years ago · Santiago Trujillo
4 Respostas
Responde à pergunta

0

@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));
    }
}
over 4 years ago · Santiago Trujillo Relatório

0

bro. Sorry for my English. Y can inject WebProperties. Next step y can do getResourses() from this properties. I hope that I help to you.

over 4 years ago · Santiago Trujillo Relatório

0

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

over 4 years ago · Santiago Trujillo Relatório

0

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);
    }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda