Spring boot introduces swagger error handling

Table of contents

1. Error description

2. Find the cause

2.1 The author's code before

 3. Problem Solving Instructions

4. Solutions

4.1 Introduce the springdoc package in pom.xml

4.2 Create a configuration file (can be omitted)

4.3 Add annotations to the controller

4.4 View interface documentation

 4.5 Common annotations


1. Error description

The swagger 2.9.2 version was introduced into the java project, but it could not be started normally, and the error was reported as follows:

2023-06-25T16:20:09.152+08:00 ERROR 38980 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed java.lang.TypeNotPresentException: Type javax.servlet.http.HttpServletRequest not present at java.base/sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:117) ~[na:na] at java.base/sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:125) ~[na:na] at java.base/sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49) ~[na:na] at java.base/sun.reflect.generics.visitor.Reifier.reifyTypeArguments(Reifier.java:68) ~[na:na] at java.base/sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:138) ~[na:na] at java.base/sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49) ~[na:na] at java.base/sun.reflect.generics.repository.ClassRepository.computeSuperInterfaces(ClassRepository.java:117) ~[na:na] at java.base/sun.reflect.generics.repository.ClassRepository.getSuperInterfaces(ClassRepository.java:95) ~[na:na] at java.base/java.lang.Class.getGenericInterfaces(Class.java:1251) ~[na:na] at org.springframework.core.ResolvableType.getInterfaces(ResolvableType.java:500) ~[spring-core-6.0.8.jar:6.0.8] at org.springframework.core.ResolvableType.as(ResolvableType.java:448) ~[spring-core-6.0.8.jar:6.0.8] at org.springframework.core.ResolvableType.forClass(ResolvableType.java:1048) ~[spring-core-6.0.8.jar:6.0.8] at org.springframework.plugin.core.config.PluginRegistriesBeanDefinitionRegistrar.getTargetType(PluginRegistriesBeanDefinitionRegistrar.java:101) ~[spring-plugin-core-2.0.0.RELEASE.jar:2.0.0.RELEASE] at org.springframework.plugin.core.config.PluginRegistriesBeanDefinitionRegistrar.registerBeanDefinitions(PluginRegistriesBeanDefinitionRegistrar.java:71) ~[spring-plugin-core-2.0.0.RELEASE.jar:2.0.0.RELEASE] at org.springframework.context.annotation.ImportBeanDefinitionRegistrar.registerBeanDefinitions(ImportBeanDefinitionRegistrar.java:86) ~[spring-context-6.0.8.jar:6.0.8] at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.lambda$loadBeanDefinitionsFromRegistrars$1(ConfigurationClassBeanDefinitionReader.java:373) ~[spring-context-6.0.8.jar:6.0.8] at java.base/java.util.LinkedHashMap.forEach(LinkedHashMap.java:729) ~[na:na] at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsFromRegistrars(ConfigurationClassBeanDefinitionReader.java:372) ~[spring-context-6.0.8.jar:6.0.8] at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:148) ~[spring-context-6.0.8.jar:6.0.8] at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:120) ~[spring-context-6.0.8.jar:6.0.8] at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:410) ~[spring-context-6.0.8.jar:6.0.8] at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:283) ~[spring-context-6.0.8.jar:6.0.8] at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:344) ~[spring-context-6.0.8.jar:6.0.8] at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:115) ~[spring-context-6.0.8.jar:6.0.8] at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:747) ~[spring-context-6.0.8.jar:6.0.8] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:565) ~[spring-context-6.0.8.jar:6.0.8] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.0.6.jar:3.0.6] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) ~[spring-boot-3.0.6.jar:3.0.6] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-3.0.6.jar:3.0.6] at org.springframework.boot.SpringApplication.run(SpringApplication.java:310) ~[spring-boot-3.0.6.jar:3.0.6] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1304) ~[spring-boot-3.0.6.jar:3.0.6] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1293) ~[spring-boot-3.0.6.jar:3.0.6] at com.aiforail.datalabel.DataLabelApplication.main(DataLabelApplication.java:22) ~[classes/:na] at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:578) ~[na:na] at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-3.0.6.jar:3.0.6] Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpServletRequest at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) ~[na:na] at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) ~[na:na] at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[na:na] at java.base/java.lang.Class.forName0(Native Method) ~[na:na] at java.base/java.lang.Class.forName(Class.java:496) ~[na:na] at java.base/java.lang.Class.forName(Class.java:475) ~[na:na] at java.base/sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:114) ~[na:na] ... 35 common frames omitted 进程已结束,退出代码0

2. Find the cause

I searched for various methods on the Internet, including cloning other people's codes, and I feel that they are exactly the same, why others can do it, but my own can't. Until I saw a blog description:

I feel like I will in an instant  , yes, it is a version compatibility issue, just do what you say

2.1 The author's code before

The spring boot version is 3+

 The swagger version is 2+

 3. Problem Solving Instructions

  • The integration of swagger 2.0 version and spring boot 3.x version is not possible for the time being, because the underlying layer of swagger depends on the javax dependency package, and spring boot 3.x version is all jakarta dependent packages, generally spring boot 2.5 and below It’s ok, but if you want to start spring boot with version 2.5 or above, you will get an error
  • Therefore, springfox can no longer be used here, and it must be replaced by something else, such as the springdoc used by the author.
     

4. Solutions

4.1 Introduce the springdoc package in pom.xml

This already contains swagger, don't worry about how to introduce swagger

        <!-- 生成接口文档 -->
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-api</artifactId>
            <version>2.0.2</version>
        </dependency>

4.2 Create a configuration file (can be omitted)

Create SpringDocConfig.java under the config folder and add the following code

@Configuration
@OpenAPIDefinition(info = @Info(title = "数据标签管理系统", description = "数据标签管理", version = "1.0"))
@SecurityScheme(name = "token", type = SecuritySchemeType.HTTP, bearerFormat = "JWT", scheme = "bearer")
public class SpringDocConfig {
}

4.3 Add annotations to the controller

4.4 View interface documentation

Enter http://localhost:9090/swagger-ui/index.html#/ in the address bar

Modify the ip and port by yourself, and then you can see the interface, which means that the swagger configuration is successful

 4.5 Common annotations

An author has already written it very clearly, so I won’t repeat it here, click here to view

Guess you like

Origin blog.csdn.net/liuxin00020/article/details/131394536