Java 如何从Spring boot后端获取Axios中的异常消息

Java 如何从Spring boot后端获取Axios中的异常消息,java,reactjs,spring-boot,exception,axios,Java,Reactjs,Spring Boot,Exception,Axios,我用了一个弹簧靴回来与反应前。axios支持Api调用。现在在SpringController中,我处理异常并抛出另一个异常,以便React从中提取消息(我真的需要这个消息)。但我得到的只是“请求失败,状态代码为500”。我怎样才能得到这个消息? 我的控制器: @DeleteMapping("/{id}") public ResponseEntity<?> delete(@PathVariable("id") Integer id) t

我用了一个弹簧靴回来与反应前。axios支持Api调用。现在在SpringController中,我处理异常并抛出另一个异常,以便React从中提取消息(我真的需要这个消息)。但我得到的只是“请求失败,状态代码为500”。我怎样才能得到这个消息? 我的控制器:

@DeleteMapping("/{id}")
    public ResponseEntity<?> delete(@PathVariable("id") Integer id) throws RuntimeException {
        log.info("Request for delete settings {}", id);
        try{
            Settings settings = settingsService.findById(id);
        settingsService.delete(settings.getId());
        log.info("SUCCESS: {} deleted.", settings.getId());
        return new ResponseEntity<>(settings.getId() + " deleted successfully.", HttpStatus.NO_CONTENT);
        } catch (RuntimeException e) {
            System.out.println("CAUSE Controller: "+e.getCause().getMessage());
            throw new RuntimeException("Error during deletion: " + e.getCause().getMessage());
        }
    }
现在我有了一个“表”组件,我重用了很多。并将删除方法作为每个组件的道具发送。 这一个来自设置组件

 <Table
                    ref='table'
                    getTr={this.getTr()}
                    getUrl={this.getUrl(idType)}
                    columns={this.getColumns()}
                    updateSelectedItem={this.updateSelectedItem}
                    deleteItem={this.deleteItem}
                    isSettings={true}
                    showDelete={showDelete}
                    toggleShowDelete={this.toggleShowDelete}
                />
在Table component中,我使用如下按钮删除:

<Button
                            variant='primary'
                            onClick={() => {
                                globalProps
                                    .deleteItem()
                                    .then((res) => {
                                        console.log(res);
                                        table.draw();
                                    })
                                    .catch((e) => {
                                        console.log(e);
                                        this.setState({ error: e.message });
                                        this.setState({ showModalBatch: true });
                                    });
                                this.props.toggleShowDelete();
                            }}
                        >
                            Delete
                        </Button>
{ statusCode: 1002, statusMessage: "My custom error message" }
{
环球酒店
.deleteItem()
。然后((res)=>{
控制台日志(res);
table.draw();
})
.catch((e)=>{
控制台日志(e);
this.setState({error:e.message});
this.setState({showModalBatch:true});
});
this.props.toggleShowDelete();
}}
>
删去
我希望这个e.message就是我在控制器中输入的那个。我该怎么做?

  • 在Axios上,您可以访问“error.response.data”中的错误响应主体
  • 在您的spring boot上,我建议根据您的需要使用它来定制您的响应
例如:

@ExceptionHandler(MyCustomException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public MyCustomResponse myCustomException(MyCustomException e) {
 return new MyCustomResponse(e.getStatusCode(), e.getStatusMessage());
}
  • 因此,在下面的示例中,您将在代码中抛出一个MyCustomException,然后在@ControllerAdvice组件中拦截它,并以JSON格式返回一个MyCustomResponse
您的自定义响应正文将如下所示:

<Button
                            variant='primary'
                            onClick={() => {
                                globalProps
                                    .deleteItem()
                                    .then((res) => {
                                        console.log(res);
                                        table.draw();
                                    })
                                    .catch((e) => {
                                        console.log(e);
                                        this.setState({ error: e.message });
                                        this.setState({ showModalBatch: true });
                                    });
                                this.props.toggleShowDelete();
                            }}
                        >
                            Delete
                        </Button>
{ statusCode: 1002, statusMessage: "My custom error message" }
因此,现在您在响应中返回此主体,您可以在“error.response.data.statusMessage”的axios上访问它

请检查示例中的http状态代码(@ResponseStatus)是否设置为500,但您可以根据自己的要求对其进行自定义。

  • 在Axios上,您可以访问“error.response.data”中的错误响应主体
  • 在您的spring boot上,我建议根据您的需要使用它来定制您的响应
例如:

@ExceptionHandler(MyCustomException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
public MyCustomResponse myCustomException(MyCustomException e) {
 return new MyCustomResponse(e.getStatusCode(), e.getStatusMessage());
}
  • 因此,在下面的示例中,您将在代码中抛出一个MyCustomException,然后在@ControllerAdvice组件中拦截它,并以JSON格式返回一个MyCustomResponse
您的自定义响应正文将如下所示:

<Button
                            variant='primary'
                            onClick={() => {
                                globalProps
                                    .deleteItem()
                                    .then((res) => {
                                        console.log(res);
                                        table.draw();
                                    })
                                    .catch((e) => {
                                        console.log(e);
                                        this.setState({ error: e.message });
                                        this.setState({ showModalBatch: true });
                                    });
                                this.props.toggleShowDelete();
                            }}
                        >
                            Delete
                        </Button>
{ statusCode: 1002, statusMessage: "My custom error message" }
因此,现在您在响应中返回此主体,您可以在“error.response.data.statusMessage”的axios上访问它

请检查示例中的http状态代码(@ResponseStatus)是否设置为500,但您可以根据自己的需要对其进行自定义