Search code examples
spring-bootswagger

How to use both @RestControllerAdvice and Swagger UI in Spring boot


I have a very simple Spring boot project contains Swagger UI:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
    <version>2.5.0</version>
</dependency>

After I setting a class has @RestControllerAdvice, trying to view swagger UI throw an exception: enter image description here

And the log is

java.lang.NoSuchMethodError: 'void org.springframework.web.method.ControllerAdviceBean.<init>(java.lang.Object)'

The tree:

└───com
    └───cuong02n
        └───test
            │   App.java
            │
            ├───controller
            │       CourseController.java
            │       StudentController.java
            │
            └───exception
                    CustomExceptionHandler.java <-- The @RestControllerAdvice here
                    StudentException.java

After reading and trying some solutions by adding basePackage, I still can't access Swagger UI.

Update

The pom.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cuong02n</groupId>
    <artifactId>test-controller-advice-swagger</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>test-controller-advice-swagger</name>
    <description>test-controller-advice-swagger</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Solution

  • I am encountering the same issue as you. I am using the following versions in my pom.xml:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.4.1</version>
        <relativePath/>
    </parent>
    
    <dependencies>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
            <version>2.7.0</version>
        </dependency>
    </dependencies>
    

    I was facing the java.lang.NoSuchMethodError,

    Caused by: java.lang.NoSuchMethodError: 'void org.springframework.web.method.ControllerAdviceBean.<init>(java.lang.Object)'
    

    but I was able to resolve it by using @Hidden from SpringDoc. Specifically, you need to add @Hidden on your @RestControllerAdvice or @ControllerAdvice classes (see the documentation here: SpringDoc - How to hide an operation or controller).

    @Slf4j
    @Hidden
    @RestControllerAdvice
    public class GlobalHandler extends ResponseEntityExceptionHandler {
    ...
    }
    

    This solution provides a temporary fix while using the versions of Spring Boot and SpringDoc that I want, without causing any errors.