@RestController
public class CustomErrorController extends BasicErrorController {
@Autowired
public CustomErrorController(ErrorAttributes errorAttributes,
ServerProperties serverProperties,
List<ErrorViewResolver> errorViewResolvers) {
super(errorAttributes, serverProperties.getError(), errorViewResolvers);
}

@RequestMapping
@Override
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
Map<String, Object> body = getErrorAttributes(request, this.isIncludeStackTrace(request, MediaType.ALL));
Boolean errorFlags = (Boolean) body.getOrDefault("errorFlags", false);
if (errorFlags) {
Integer statusI = (Integer) body.getOrDefault("status",500);
return new ResponseEntity(body, HttpStatus.valueOf(statusI));
}
return super.error(request);
}
}


@Component
public class CustomErrorAttributes extends DefaultErrorAttributes {
@Override
public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes, boolean includeStackTrace) {
final Throwable error = super.getError(requestAttributes);
if (error instanceof ForbiddenException) {
Map<String, Object> errorAttributes = new LinkedHashMap();
errorAttributes.put("timestamp", new Date());
errorAttributes.put("errorFlags", true);
errorAttributes.put("status", ForbiddenException.STATUS);
errorAttributes.put("message", error.getMessage());
return errorAttributes;
} else {
return super.getErrorAttributes(requestAttributes, includeStackTrace);
}
}
}