Error cause: it may be caused by version mismatch. My spring boot is 2.6.0, but the spring cloud version needs to be compared in this way and cannot be degraded, so it is configured in this way
POM dependency
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>25.1-jre</version>
</dependency>
</dependencies>
Startup class:
@SpringBootApplication
@EnableDiscoveryClient
@EnableWebMvc // Comment out
public class UploadServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UploadServiceApplication.class, args);
}
}
This configuration may cause the UI page of swagger to fail to open, so the following configuration is required:
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {
/**
* Accessing static resources
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
/**
* SpringBoot autoconfiguration itself does not map the /swagger-ui.html
* This path is mapped to the corresponding directory META-INF/resources/
* Use WebMvcConfigurerAdapter to publish the static files of swagger;
*/
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
//Map all /static/** accesses to the classpath:/static/ directory
registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX +"/static/");
super.addResourceHandlers(registry);
}
}