Swagger3-ui URL在我的Spring Boot项目中不起作用



我也试过这些网址

http://localhost: 8080/swagger-ui/index . html

http://localhost: 8080/swagger-ui/

这是我的swagger配置类

@Configuration
public class SpringFoxConfig {
@Bean
public Docket apiDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.faramarz.spring.Covid19RestApi"))
.paths(PathSelectors.any())
.build()
.apiInfo(getApiInfo());
}
private ApiInfo getApiInfo() {
return new ApiInfo(
"TITLE",
"DESCIPRION",
"VERSION",
"TERMS OF SERVICE URL",
new Contact("NAME", "URL", "EMAIL"),
"LICENSE",
"LICENSE URL",
Collections.emptyList()
);
}
}

pom.xml

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>

正确的url是

http://localhost:8080/swagger-ui.html

添加此注释

@EnableSwagger2

添加这两个依赖项,并删除您一直在使用的上述依赖项

<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>

this will work

@Configuration
@EnableSwagger2
public class SpringFoxConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.paths(PathSelectors.any())
.build()
.globalOperationParameters(parameters());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("USER API")
.contact(new Contact("Your Name", "", "Your Email"))
.build();
}
private List<Parameter> parameters() {
List<Parameter> parameters = new ArrayList<>();
Parameter headParameter = new ParameterBuilder()
.name("User Restful API")
.description("")
.modelRef(new ModelRef("string"))
.parameterType("header")
.required(false)
.build();
parameters.add(headParameter);
return parameters;
}}

请使用这个类,只是注释你的类和测试这个

终于有一天,我修复了这个问题:

@Configuration
@EnableSwagger2WebMvc
public class SwaggerDocumentation {
public static final Contact CONTACT = new Contact("Faramarz", "http://muralitechblog.com/", "https://faramarzaf.github.io/");
public static final ApiInfo DEFAULT_API = new ApiInfo("swagger", "Swagger Documentation", "1.0", "urn:tos", CONTACT, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList<>());
public static final Set<String> consumes = new HashSet<>(Arrays.asList("application/json"));
public static final Set<String> produces = new HashSet<>(Arrays.asList("application/json"));
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(DEFAULT_API).consumes(consumes).produces(produces);
}
}

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>

最新更新