Spring reactive web:

Earlier in spring boot 2.2.*, i was using a class that extends DefaultErrorAttributes. This class was used to handle exceptions globally for the overall micro service. When i upgrade to 2.3.1, it's not working anymore. I don't find any major change in spring reactive web in 2.3.1 version. Is there any change that breaks this? Anything we need to change? Any input?

it seems like DefaultErrorAttributes is not called anymore. Sample code is here.

@Slf4j
@Component
public class GlobalErrorAttributes extends DefaultErrorAttributes{`

@Override
public Map<String, Object> getErrorAttributes(ServerRequest request, 
  boolean includeStackTrace) {
    Map<String, Object> map = super.getErrorAttributes(
      request, includeStackTrace);
    map.put("status", HttpStatus.BAD_REQUEST);
    map.put("message", "username is required");
    return map;
}

}

2

There are 2 answers

0
otidh On

I'm facing the same issue in Spring Boot 2.4.0. I solved it by following the workaround in this link https://github.com/spring-projects/spring-security/issues/4467. Adding:

.antMatchers("/error").permitAll()
0
merc-angel On

I found Your answer and this works for me

@Component
@Slf4j
public class ErrorAttributes extends DefaultErrorAttributes
{
    @Override
    public Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
        
        var attributes = new LinkedHashMap<String, Object>();
        attributes.put("status", HttpStatus.BAD_REQUEST.value());
        attributes.put("message", "bad");
        return attributes;
    }


}

I have Spring Boot in 2.3.1 version