錯誤原因: 可能是版本不匹配造成,我的 spring boot 是 2.6.0 而 spring cloud 版本需要這樣對照, 不能進行降級, 所以這樣配置.
pom依賴
<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>
啟動類:
@SpringBootApplication
@EnableDiscoveryClient
@EnableWebMvc // 需要此注解
public class UploadServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UploadServiceApplication.class, args);
}
}
這樣配置可能會造成 swagger 的 ui 頁面無法打開所以需要下面配置,這個是在 https://blog.csdn.net/lovequanquqn/article/details/90705721
博客中看到的
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 {
/**
* 訪問靜態資源
* */
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
/**
* SpringBoot自動配置本身並不會把/swagger-ui.html
* 這個路徑映射到對應的目錄META-INF/resources/下面
* 采用WebMvcConfigurerAdapter將swagger的靜態文件進行發布;
*/
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
//將所有/static/** 訪問都映射到classpath:/static/ 目錄下
registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX +"/static/");
super.addResourceHandlers(registry);
}
}