Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.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/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 4.0.1中配置不正确时工作_Java_Spring_Spring Mvc_Exception Handling - Fatal编程技术网

Java @控制器建议在spring 4.0.1中配置不正确时工作

Java @控制器建议在spring 4.0.1中配置不正确时工作,java,spring,spring-mvc,exception-handling,Java,Spring,Spring Mvc,Exception Handling,我正在尝试使用@ControllerAdvice for Rest API调用处理和响应带有自定义错误(消息)的请求 尝试配置和设置,并在文档中指定,效果不佳。这是我的代码片段 在RestController中,方法具有以下语句 if(object==null){ throw new CustomGenericException("E_001", "Null error"); } CustomGenericeException类如下所示 public class CustomGe

我正在尝试使用@ControllerAdvice for Rest API调用处理和响应带有自定义错误(消息)的请求

尝试配置和设置,并在文档中指定,效果不佳。这是我的代码片段

在RestController中,方法具有以下语句

if(object==null){   
    throw new CustomGenericException("E_001", "Null error");
}
CustomGenericeException类如下所示

public class CustomGenericException extends RuntimeException {

private static final long serialVersionUID = 1L;

private String errCode;
private String errMsg;

public String getErrCode() {
    return errCode;
}

public void setErrCode(String errCode) {
    this.errCode = errCode;
}

public String getErrMsg() {
    return errMsg;
}

public void setErrMsg(String errMsg) {
    this.errMsg = errMsg;
}

public CustomGenericException(String errCode, String errMsg) {
    this.errCode = errCode;
    this.errMsg = errMsg;
}
}
@ControllerAdvice
public class GlobalExceptionController{

    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    @ExceptionHandler(CustomGenericException.class)
    public @ResponseBody String handleCustomException(CustomGenericException ex) {
        System.out.println(ex.getErrCode());
        return ex.getErrMsg();
    }
}
自定义异常处理程序类如下所示

public class CustomGenericException extends RuntimeException {

private static final long serialVersionUID = 1L;

private String errCode;
private String errMsg;

public String getErrCode() {
    return errCode;
}

public void setErrCode(String errCode) {
    this.errCode = errCode;
}

public String getErrMsg() {
    return errMsg;
}

public void setErrMsg(String errMsg) {
    this.errMsg = errMsg;
}

public CustomGenericException(String errCode, String errMsg) {
    this.errCode = errCode;
    this.errMsg = errMsg;
}
}
@ControllerAdvice
public class GlobalExceptionController{

    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    @ExceptionHandler(CustomGenericException.class)
    public @ResponseBody String handleCustomException(CustomGenericException ex) {
        System.out.println(ex.getErrCode());
        return ex.getErrMsg();
    }
}
最后,我在dispatcher-servlet.xml文件中有如下配置

<mvc:annotation-driven />
<context:component-scan base-package="com.xyz.pqr.controller" />

我找到了解决方案,不要在试图抛出自定义错误的方法中使用try/catch块。它可以工作。

你确定你的控制器建议会被调用吗?根据在线教程,这是要进行的配置,我确实做到了。SOP没有打印意味着它没有被调用。。你能帮我修一下吗?你的建议放在哪里了?在我的意思是什么包中,如果您添加配置并描述您的项目结构,它将非常有用。我将Advisor放在controller包中