Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

211
Views
Código Spring Boot mínimo para registrar 404

En mi aplicación Spring Boot, actualmente estoy aprovechando la página de error personalizada resources/public/error/404.html para mostrar (automágicamente) los errores de esta página 404 en URL no válidas.

¿Hay una manera fácil de conservar esta funcionalidad automática y agregar un mensaje de registro simple (con la URL no válida) para cada 404?

Idealmente, con la menor cantidad de código posible, me gustaría algo como:

 //Some code LOGGER.warn("Invalid URL " + request.url); //Some more code
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Debe definir un ErrorViewResolver personalizado:

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

Este MyErrorViewResolver se llamará automáticamente en la clase BasicErrorController.

Para un error 404, se mostrará la vista "error404".

Para los demás errores, se mostrará la vista "error".

Las vistas deben estar en la carpeta "plantillas" (resources/templates/error404.html).

about 4 years ago · Santiago Trujillo Report

0

Agregue el controlador NotFoundException.

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!