Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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 Spring引导rest控制器错误消息始终为空_Java_Json_Spring_Spring Boot_Spring Restcontroller - Fatal编程技术网

Java Spring引导rest控制器错误消息始终为空

Java Spring引导rest控制器错误消息始终为空,java,json,spring,spring-boot,spring-restcontroller,Java,Json,Spring,Spring Boot,Spring Restcontroller,当调用rest控制器时,最终引发错误。然而,我的问题是,消息字段始终为空,即使我向异常提供消息。错误响应JSON如下所示: { "timestamp": "2020-12-23T21:44:30.077+00:00", "status": 500, "error": "Internal Server Error", "message": &quo

当调用rest控制器时,最终引发错误。然而,我的问题是,
消息
字段始终为空,即使我向异常提供消息。错误响应JSON如下所示:

{
    "timestamp": "2020-12-23T21:44:30.077+00:00",
    "status": 500,
    "error": "Internal Server Error",
    "message": "",
    "path": "/v1/example"
}
在我的代码中,我抛出了如下异常:

throw new RuntimeException("HELLO!");
但是字符串msg参数从未出现在响应中


我知道我可以编写自己的错误处理程序,我正在这样做。但是,如果发生我没有准备或处理的错误,我希望在响应中显示错误消息,如果不是这样,Spring boot error json中的
消息
字段有什么意义?

您的代码是正确的。调试并确保正在触发
运行时异常。如果触发,它肯定会抛出
RuntimeException
参数中提到的错误消息

我的猜测是,您的代码在RuntimeException行之前出现了一个错误,因此它不会抛出错误消息

使用这种方式抛出错误消息

@PostMapping(value = "/test-api")
public ResponseEntity<?> sampleMethod(@RequestBody UserDto userDto) {
    try {
        // Sample code
    } catch (RuntimeException e) {
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
    }
}:
服务类的某个位置添加以下代码

throw new RuntimeException("Hello");
在控制器类中捕获异常并抛出异常

catch (RuntimeException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Hello!");
        }
样本完整API

@PostMapping(value = "/test-api")
public ResponseEntity<?> sampleMethod(@RequestBody UserDto userDto) {
    try {
        // Sample code
    } catch (RuntimeException e) {
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
    }
}:
@PostMapping(value=“/testapi”)
public ResponseEntity sampleMethod(@RequestBody UserDto UserDto){
试一试{
//示例代码
}捕获(运行时异常e){
返回ResponseEntity.status(HttpStatus.BAD_请求).body(e.getMessage());
}
}:

您的代码是正确的。调试并确保正在触发
运行时异常。如果触发,它肯定会抛出
RuntimeException
参数中提到的错误消息

我的猜测是,您的代码在RuntimeException
行之前出现了一个错误,因此它不会抛出错误消息

使用这种方式抛出错误消息

@PostMapping(value = "/test-api")
public ResponseEntity<?> sampleMethod(@RequestBody UserDto userDto) {
    try {
        // Sample code
    } catch (RuntimeException e) {
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
    }
}:
服务类的某个位置添加以下代码

throw new RuntimeException("Hello");
在控制器类中捕获异常并抛出异常

catch (RuntimeException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Hello!");
        }
样本完整API

@PostMapping(value = "/test-api")
public ResponseEntity<?> sampleMethod(@RequestBody UserDto userDto) {
    try {
        // Sample code
    } catch (RuntimeException e) {
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
    }
}:
@PostMapping(value=“/testapi”)
public ResponseEntity sampleMethod(@RequestBody UserDto UserDto){
试一试{
//示例代码
}捕获(运行时异常e){
返回ResponseEntity.status(HttpStatus.BAD_请求).body(e.getMessage());
}
}:

尝试抛出ResponseStatusException

throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "HELLO!");

尝试抛出ResponseStatusException

throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "HELLO!");

查找
@ControllerAdvice
查找
@ControllerAdvice