[英]Custom Exception throw in Spring Boot Application
I Want to create some custom exception in my spring boot application using @ControllerAdvice
and @ExceptionHandler
but I can get exception details as a json我想使用
@ControllerAdvice
和@ExceptionHandler
在我的 spring boot 应用程序中创建一些自定义异常,但我可以将异常详细信息作为 json
I tried every thing could not find the solution.我尝试了一切都找不到解决方案。
ErrorDetails
is a class where i used setter and getter and StringNotFoundException
is class which extends Exception ErrorDetails
是我使用 setter 和 getter 的类,而StringNotFoundException
是扩展 Exception 的类
@ControllerAdvice()
public class RestExceptionHandler {
@Autowired
Environment environment;
@ResponseStatus(value = HttpStatus.NOT_FOUND)
@ExceptionHandler(StringNotFoundException.class)
@ResponseBody
public ErrorDetails handleNotFoundException(StringNotFoundException ex, HttpServletRequest request) {
ErrorDetails ed = new ErrorDetails();
ed.setErrorCode("10000");
ed.setErrorMessage(ex.getMessage());
ed.setErrorDescription("Try");
ed.setPort(environment.getProperty("local.server.port"));
ed.setUrl(request.getRequestURI().toString());
System.out.println(ed + "****************");
return ed;}}
In controller I using this:在控制器中我使用这个:
if (companyList.isEmpty()) {
throw new StringNotFoundException();
}
Exception details shows on console but i could not get json format异常详细信息显示在控制台上,但我无法获取 json 格式
com.ffi.financialcompany.exception.StringNotFoundException
com.ffi.financialcompany.exception.StringNotFoundException
Try making RestExceptionHandler extends ResponseEntityExceptionHandler.尝试让 RestExceptionHandler 扩展 ResponseEntityExceptionHandler。
ResponseEntityExceptionHandler is a class used by Spring framework to throw Exceptions. ResponseEntityExceptionHandler 是 Spring 框架用来抛出异常的类。 I think you should extend that class in order to work this out.
我认为您应该扩展该课程以解决此问题。
@ControllerAdvice()
public class RestExceptionHandler extends ResponseEntityExceptionHandler {
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.