如何获取特定错误而不是内部服务器错误?
How to get specific error instead of Internal Server Error?
我在邮递员上收到内部服务器错误,即使我从我的代码异常中抛出自定义异常。
我想查看我抛出的具有有效错误消息和错误代码的异常。如果你们中的任何人能在这一点上帮助我,那将是一个很大的帮助。就像我怎样才能得到更好的错误信息。添加以下代码快照。
提前致谢。
@Service
public class FetchActionImpl implements FetchAction {
private static Logger log = LoggerFactory.getLogger(FetchActionImpl.class);
@Autowired
FetchActionServiceImpl fetchActionService;// = new FetchActionServiceImpl();
@Override
public FetchResponse fetchActionRequest(String caseId) throws BBWException,Exception{
//String resp ="";
log.info("fetchaction Request: {}",ApplicationConstants.LOG_ENTRY_MESSAGE);
log.info("The caseId received from BRASS:\n {}",caseId);
FetchResponse resp = null;
try{
if(true) {
throw new BBWException("500","Test");
}
resp = fetchActionService.fetchIt(caseId);
log.debug("fetchaction Response: {}",resp.toString());
}
catch (BBWException be) {
throw be;
}
catch (Exception e) {
throw new BBWException("500",e.getMessage());
}
return resp;
}
}
@Api
@Path("/fetch_service")
public interface FetchAction {
@GET
@Path("/fetchaction/caseid/{caseid}")
@Produces({MediaType.APPLICATION_JSON})
//@Consumes({MediaType.TEXT_XML})
@ApiOperation(
value = "Respond BRASS Request",
notes = "Returns a XML object "
)
@ApiResponses(
value = {
@ApiResponse(code = 404, message = "Service not available"),
@ApiResponse(code = 500, message = "Unexpected Runtime error")
})
public FetchResponse fetchActionRequest(@PathParam("caseid") String caseid) throws BBWException, Exception;
}`
public class BBWException extends Exception {
private static final long serialVersionUID = -7987978270260338068L;
private String errorCode;
private String errorMessage;
public BBWException(String errorCode, String errorMessage) {
super(errorMessage);
this.errorCode = errorCode;
this.errorMessage = errorMessage;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
@Override
public String toString() {
return (this.errorCode + " " + this.errorMessage);
}
}
每次抛出(未捕获的)异常,SpringBoot returns Http-500 Internal Server Error
。 Spring.
中有很多处理异常的方法
假设我有我的控制器,我隐式地抛出了一个异常。
@RestController
public class HelloWorldController {
@GetMapping("/hello-world")
public String helloWorld() {
throw new MyCustomException("I just can't say hello!");
}
}
这和你的一样——你可以在异常中指定任何内容。
第一个:
处理它的方法之一是创建一个带有 @ControllerAdvice
注释的 class。
@ControllerAdvice
public class GlobalExceptionHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(MyCustomException.class)
public ResponseEntity<String> handlyMyCustomException(MyCustomException e) {
logger.error("error occurred {}", e);
return new ResponseEntity<>("Something happened: " + e.getMessage(), HttpStatus.I_AM_A_TEAPOT);
}
}
通过这种方式,您可以捕获您选择的异常(全局)和 return 具有您选择的 HTTP 响应状态的消息,不一定 I_AM_A_TEAPOT
第二个:
@ExceptionHandler(MyCustomException.class)
public ResponseEntity<String> handlyMyCustomException(MyCustomException e) {
logger.error("error occurred {}", e);
return new ResponseEntity<>("Something happened: " + e.getMessage(), HttpStatus.I_AM_A_TEAPOT);
}
您也可以在您的控制器 class 中仅创建用 @ExceptionHandler
注释的方法 - 但这不是全局的,并且仅适用于这个确切的控制器调用。
结果如下:
第三个:
另一种处理异常的方法是创建您自己的错误 .html
文件。如果您将文件放在 resources/static/error/500.html
中,它应该在抛出 Http-500 Internal Server Error
时被 returned。
我在邮递员上收到内部服务器错误,即使我从我的代码异常中抛出自定义异常。 我想查看我抛出的具有有效错误消息和错误代码的异常。如果你们中的任何人能在这一点上帮助我,那将是一个很大的帮助。就像我怎样才能得到更好的错误信息。添加以下代码快照。 提前致谢。
@Service
public class FetchActionImpl implements FetchAction {
private static Logger log = LoggerFactory.getLogger(FetchActionImpl.class);
@Autowired
FetchActionServiceImpl fetchActionService;// = new FetchActionServiceImpl();
@Override
public FetchResponse fetchActionRequest(String caseId) throws BBWException,Exception{
//String resp ="";
log.info("fetchaction Request: {}",ApplicationConstants.LOG_ENTRY_MESSAGE);
log.info("The caseId received from BRASS:\n {}",caseId);
FetchResponse resp = null;
try{
if(true) {
throw new BBWException("500","Test");
}
resp = fetchActionService.fetchIt(caseId);
log.debug("fetchaction Response: {}",resp.toString());
}
catch (BBWException be) {
throw be;
}
catch (Exception e) {
throw new BBWException("500",e.getMessage());
}
return resp;
}
}
@Api
@Path("/fetch_service")
public interface FetchAction {
@GET
@Path("/fetchaction/caseid/{caseid}")
@Produces({MediaType.APPLICATION_JSON})
//@Consumes({MediaType.TEXT_XML})
@ApiOperation(
value = "Respond BRASS Request",
notes = "Returns a XML object "
)
@ApiResponses(
value = {
@ApiResponse(code = 404, message = "Service not available"),
@ApiResponse(code = 500, message = "Unexpected Runtime error")
})
public FetchResponse fetchActionRequest(@PathParam("caseid") String caseid) throws BBWException, Exception;
}`
public class BBWException extends Exception {
private static final long serialVersionUID = -7987978270260338068L;
private String errorCode;
private String errorMessage;
public BBWException(String errorCode, String errorMessage) {
super(errorMessage);
this.errorCode = errorCode;
this.errorMessage = errorMessage;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
@Override
public String toString() {
return (this.errorCode + " " + this.errorMessage);
}
}
每次抛出(未捕获的)异常,SpringBoot returns Http-500 Internal Server Error
。 Spring.
假设我有我的控制器,我隐式地抛出了一个异常。
@RestController
public class HelloWorldController {
@GetMapping("/hello-world")
public String helloWorld() {
throw new MyCustomException("I just can't say hello!");
}
}
这和你的一样——你可以在异常中指定任何内容。
第一个:
处理它的方法之一是创建一个带有 @ControllerAdvice
注释的 class。
@ControllerAdvice
public class GlobalExceptionHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(MyCustomException.class)
public ResponseEntity<String> handlyMyCustomException(MyCustomException e) {
logger.error("error occurred {}", e);
return new ResponseEntity<>("Something happened: " + e.getMessage(), HttpStatus.I_AM_A_TEAPOT);
}
}
通过这种方式,您可以捕获您选择的异常(全局)和 return 具有您选择的 HTTP 响应状态的消息,不一定 I_AM_A_TEAPOT
第二个:
@ExceptionHandler(MyCustomException.class)
public ResponseEntity<String> handlyMyCustomException(MyCustomException e) {
logger.error("error occurred {}", e);
return new ResponseEntity<>("Something happened: " + e.getMessage(), HttpStatus.I_AM_A_TEAPOT);
}
您也可以在您的控制器 class 中仅创建用 @ExceptionHandler
注释的方法 - 但这不是全局的,并且仅适用于这个确切的控制器调用。
结果如下:
第三个:
另一种处理异常的方法是创建您自己的错误 .html
文件。如果您将文件放在 resources/static/error/500.html
中,它应该在抛出 Http-500 Internal Server Error
时被 returned。