Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

212
Vistas
Minimum Spring Boot Code to log 404s

In my Spring Boot Application, I'm currently leveraging the resources/public/error/404.html custom error page to show (automagically) this 404 page errors on invalid URLS.

Is there an easy way to retain this auto functionality, and add a simple log message (with the invalid URL) for every such 404 ?

Ideally with as little code as possible I would want some like :

//Some code
LOGGER.warn("Invalid URL " + request.url);
//Some more code
about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You need to define a custom ErrorViewResolver:

@Component
public class MyErrorViewResolver implements ErrorViewResolver {

    @Override
    public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map<String, Object> model) {
    if (HttpStatus.NOT_FOUND == status) {
        LoggerFactory.getLogger(this.getClass()).error("error 404 for url " + model.get("path"));
        return new ModelAndView("error404", model);
    }
    else {
        return new ModelAndView("error", model);
    }
  }
}

This MyErrorViewResolver will be automatically called in the BasicErrorController class.

For a 404 error, the view "error404" will be displayed.

For the other errors, the view "error" will be displayed.

Views must be in the "templates" folder (resources/templates/error404.html).

about 4 years ago · Santiago Trujillo Denunciar

0

Add NotFoundException handler.

public class BaseController {

  // Logger declaration

  @ExceptionHandler(NotFoundException.class)
  @ResponseStatus(value = HttpStatus.NOT_FOUND)
  @ResponseBody
  public ErrorResponse handleNotFoundError(HttpServletRequest req, NotFoundException exception) {
     List<Error> errors = Lists.newArrayList();
     errors.add(new Error(String.valueOf(exception.getCode()), exception.getMessage()));
     log.error(exception);
     return new ErrorResponse(errors);
  }
}
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda