Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.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
如何在React.js中显示服务器(Java)异常消息_Java_Reactjs_Spring_Rest_React Redux - Fatal编程技术网

如何在React.js中显示服务器(Java)异常消息

如何在React.js中显示服务器(Java)异常消息,java,reactjs,spring,rest,react-redux,Java,Reactjs,Spring,Rest,React Redux,我无法看到抛出异常时(由Java中的服务器)设置的消息(在React.js catch块中) 情况:用户希望执行某些操作(通过myService),某些验证w.r.t操作只能在后端执行,如果失败,如何向用户显示失败的原因 服务(Java): 操作(React.js): 希望通过在动作的catch块中的按摩中设置值来显示异常消息 输出 err.data: "" err.message: undefined 而且 请提供帮助。如果出现异常,默认情况下,Spring返回http状态500,内容类型:

我无法看到抛出异常时(由Java中的服务器)设置的消息(在React.js catch块中)

情况:用户希望执行某些操作(通过myService),某些验证w.r.t操作只能在后端执行,如果失败,如何向用户显示失败的原因

服务(Java):

操作(React.js):

希望通过在动作的catch块中的按摩中设置值来显示异常消息

输出

err.data: ""
err.message: undefined
而且


请提供帮助。

如果出现异常,默认情况下,Spring返回http状态500,内容类型:text/html;charset=UTF-8并生成带有错误描述的html页面。您的错误描述将出现在本页的最后一行

<div>There was an unexpected error (type=Internal Server Error, status=500).</div>
然后您将得到内容类型为:application/json的响应,如下所示

<div>This is error message</div>
{
    "code": 500,
    "message": "This is error message"
} 

您可以使用
ResponseStatusException
reactjs
发送错误消息

@RequestMapping(method = RequestMethod.GET, value = "/get")
public List<MyInfo> getInfo(){
   try {
          // code to return
       } catch (InvalidObjectException | InvalidOperationException | OperationNotPermittedException | UserPermissionNotAvailableException e) {
    // throwing custom exceptions
    throw new ResponseStatusException(HttpStatus.FORBIDDEN, e.getMessage(), e);
        } 
   catch (Exception e) {
        throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage(), e);
       }
   }

更多信息:您好,谢谢您的回复。根据需要在
例外回复
中添加
构造函数
。CE在
@Value
,“此位置不允许注释@Value”。为了简洁起见,我使用了
@Value
。您可以用构造函数和getter替换它。或者您必须将lombok插件安装到您的IDE中。我测试了我在我的机器上发布的代码样本,所以一切都应该正常。删除了@Value。调试了代码,我可以看到正在执行方法
handleCustomException
,但在UI中看不到消息。您能告诉我对象
err
中属性
消息的完整路径是什么吗
code
的getter/setter,解决上述问题需要
message
。请确保@Value注释来自lombok,而不是Spring。您还可以查看—或使用工作示例—感谢您的回复。找不到名为
ResponseStatusException
的任何类型。你能指定包裹吗?请看底部我在答案中包含的链接。从5.0开始,它就是Spring框架的一部分。
ResponseStatusException
从5.0开始,但我们使用的是4.3.x。您可以在这里查看其他解决方案,包括较旧的Spring版本:这可能是相同的问题吗?
<div>This is error message</div>
import lombok.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ErrorHandler {

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ExceptionRestResponse handleCustomException(Exception exception) {
        return new ExceptionRestResponse(500, exception.getMessage());
    }

    @Value
    public static class ExceptionRestResponse {
        int code;
        String message;
    }
} 
{
    "code": 500,
    "message": "This is error message"
} 
@RequestMapping(method = RequestMethod.GET, value = "/get")
public List<MyInfo> getInfo(){
   try {
          // code to return
       } catch (InvalidObjectException | InvalidOperationException | OperationNotPermittedException | UserPermissionNotAvailableException e) {
    // throwing custom exceptions
    throw new ResponseStatusException(HttpStatus.FORBIDDEN, e.getMessage(), e);
        } 
   catch (Exception e) {
        throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage(), e);
       }
   }
"error" : {
     "status": 403,
     "statusText": "FORBIDDEN",
     "message": "User does not have permission to perform this operation",
     "timestamp": "2020-05-08T04:28:32.917+0000"
     ....
}