简体   繁体   English

Spring Boot 应用程序中的自定义异常抛出

[英]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.

相关问题 在Spring-Boot中为soap webservice抛出自定义异常? - Throw custom exception for soap webservice in Spring-Boot? Spring 启动应用程序中的自定义异常失败 - Custom Exception in Spring boot Application Fails Spring 引导 - 抛出异常或指示未找到项目 - Spring Boot - throw exception or indicate item is not found 如何在 Spring Boot 的 JSON 中抛出异常 - How to throw an exception back in JSON in Spring Boot Spring Boot Rest应用程序中的RxJava自定义异常处理/传播 - RxJava custom exception handling/propagation in spring boot rest application 在 Spring Boot 应用程序中防止自定义异常的堆栈跟踪日志记录 - Prevent stack trace logging for custom exception in Spring Boot application Spring Boot Application中的自定义异常调度程序用于响应逻辑 - Custom Exception Dispatcher in Spring Boot Application for response logic PowerMock 不会模拟静态方法在 Spring-Boot 应用程序中抛出异常 - PowerMock won't mock static method to throw an Exception in a Spring-Boot application spring-boot 如何在基于 Jax-RS 响应 Httpstatus 的 try 块中抛出多个自定义异常 - spring-boot how to throw multiple custom exception in try block based on Jax-RS response Httpstatus 使用 Hibernate Validator 在 Spring Boot 中进行 Bean 验证失败时如何抛出自定义异常? - How to Throw a Custom Exception When Bean Validation In Spring Boot with Hibernate Validator Fails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM