Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为未找到的资源创建自定义异常404 SPRING BOOT_Java_Spring_Spring Mvc_Spring Boot_Java 8 - Fatal编程技术网

Java 为未找到的资源创建自定义异常404 SPRING BOOT

Java 为未找到的资源创建自定义异常404 SPRING BOOT,java,spring,spring-mvc,spring-boot,java-8,Java,Spring,Spring Mvc,Spring Boot,Java 8,我确实有一个类CustomResponseEntityExceptionHandler扩展类ResponseEntityExceptionHandler,它有两个方法,一个用于处理错误格式,另一个用于请求资源(url)但不存在时。我想给用户一条自定义消息,而不是Spring默认的白色标签错误页面我试图理解为什么在请求不存在的URI时,CustomResponseEntityExceptionHandler中的handleResourceNotFoundException方法没有被调用,但我一直保

我确实有一个类
CustomResponseEntityExceptionHandler
扩展类
ResponseEntityExceptionHandler
,它有两个方法,一个用于处理错误格式,另一个用于请求资源(url)但不存在时。我想给用户一条自定义消息,而不是Spring默认的白色标签错误页面我试图理解为什么在请求不存在的URI时,
CustomResponseEntityExceptionHandler
中的
handleResourceNotFoundException
方法没有被调用
,但我一直保持白色标签错误页面。谢谢你的帮助

curl localhost:8080/doesnotexist
这是我的
CustomResponseEntityExceptionHandler

@ExceptionHandler(Exception.class)
public final ResponseEntity<ErrorDetails> handleAllWrongFormatExceptions(WrongFormatException ex,
        WebRequest request) {
    ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(true));
    return new ResponseEntity<>(errorDetails, HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler(ResourceNotFoundException.class)
public final ResponseEntity<ErrorDetails> handleResourceNotFoundException(ResourceNotFoundException ex,
        WebRequest request) {
    ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
    return new ResponseEntity<>(errorDetails, HttpStatus.NOT_FOUND);
}
这是我的控制器

@Controller
public class StringManipController {

@Autowired
private StringProcessingService stringProcessingService;

@GetMapping("/")
@ExceptionHandler(ResourceNotFoundException.class)
@ResponseBody
public String home(@RequestParam(name = "value", required = false, defaultValue = "") String value) {

    return "Welcome to the String Processing Application";
}

@GetMapping("/stringDedup")
@ResponseBody
public ProcessedString doManip(@RequestParam(name = "value", required = false, defaultValue = "") String value) {

    String result = stringProcessingService.getStringManipulation(value);

    return new ProcessedString(result);

}

@GetMapping("/writeNumber")
@ResponseBody
public ProcessedString getWriteNumber(
        @RequestParam(name = "value", required = false, defaultValue = "") String value) {

    String number = stringProcessingService.getNumberToWords(value);

    return new ProcessedString(number);

} }
这里是
ResourceNotFoundException类

    @ResponseStatus(HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {


    private static final long serialVersionUID = 266853955330077478L;

    public ResourceNotFoundException(String exception) {
        super(exception);
    } }

如果您使用了Spring Boot:

@ControllerAdvice
public class CustomResponseEntityExceptionHandler {

    @ExceptionHandler(value = { NoHandlerFoundException.class })
    public ResponseEntity<Object> noHandlerFoundException(Exception ex) {

        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("test");
    }
}
@ControllerAdvice
公共类CustomResponseEntityExceptionHandler{
@ExceptionHandler(值={NoHandlerFoundException.class})
公共响应性noHandlerFoundException(异常ex){
返回响应状态(HttpStatus.BAD_请求)。正文(“测试”);
}
}

@ControllerAdvice
公共类CustomResponseEntityExceptionHandler扩展了ResponseEntityExceptionHandler{
@凌驾
受保护的响应handleNoHandlerFoundException(
NoHandlerFoundException ex、HttpHeaders标头、HttpStatus状态、WebRequest请求){
返回handleExceptionInternal(例如,“测试”、标题、状态、请求);
}
}
org.springframework.web.servlet.DispatcherServlet
类中,有一个名为
ThroweExceptionIfNoHandlerFound
的变量。如果将其设置为
true
,则名为
noHandlerFound
的方法将抛出
NoHandlerFoundException
。您的异常处理程序现在将捕获它


将此属性添加到您的
应用程序.properties
文件:
spring.mvc。如果未找到处理程序,则抛出异常=true

如果使用spring引导:

@ControllerAdvice
public class CustomResponseEntityExceptionHandler {

    @ExceptionHandler(value = { NoHandlerFoundException.class })
    public ResponseEntity<Object> noHandlerFoundException(Exception ex) {

        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("test");
    }
}
@ControllerAdvice
公共类CustomResponseEntityExceptionHandler{
@ExceptionHandler(值={NoHandlerFoundException.class})
公共响应性noHandlerFoundException(异常ex){
返回响应状态(HttpStatus.BAD_请求)。正文(“测试”);
}
}

@ControllerAdvice
公共类CustomResponseEntityExceptionHandler扩展了ResponseEntityExceptionHandler{
@凌驾
受保护的响应handleNoHandlerFoundException(
NoHandlerFoundException ex、HttpHeaders标头、HttpStatus状态、WebRequest请求){
返回handleExceptionInternal(例如,“测试”、标题、状态、请求);
}
}
org.springframework.web.servlet.DispatcherServlet
类中,有一个名为
ThroweExceptionIfNoHandlerFound
的变量。如果将其设置为
true
,则名为
noHandlerFound
的方法将抛出
NoHandlerFoundException
。您的异常处理程序现在将捕获它


将此属性添加到您的
应用程序.properties
文件:
spring.mvc.throw exception if not handler found=true

请参阅我使用spring引导2。就我而言,这不是一个解决方案
ResourceHttpRequestHandler
在(resource==null){logger.debug(“resource not found”);response.sendError(HttpServletResponse.SC_not_found);return;}之前运行,然后执行下一个操作。您可以提供实现的ResponseEntityExceptionHandler有许多方法,其中之一是NoHandlerFoundException,完整列表:。请尝试设置spring.resources.add mappings=false我使用spring引导2。就我而言,这不是一个解决方案
ResourceHttpRequestHandler
在(resource==null){logger.debug(“resource not found”);response.sendError(HttpServletResponse.SC_not_found);return;}之前运行,然后执行下一个操作。您可以提供实现的ResponseEntityExceptionHandler有许多方法,其中之一是NoHandlerFoundException,完整列表:。还可以尝试设置spring.resources.add mappings=false
@ControllerAdvice
public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

    @Override
    protected ResponseEntity<Object> handleNoHandlerFoundException(
            NoHandlerFoundException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {

        return handleExceptionInternal(ex, "Test", headers, status, request);
    }
}