Swagger3-ui URL 在我的 Spring 引导项目中不起作用
Swagger3-ui URL does not working in my Spring Boot project
我也试过这些网址
http://localhost:8080/swagger-ui/index.html
http://localhost:8080/swagger-ui/
这是我的招摇配置class。
@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>
这会起作用
@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;
}}
请使用这个 Class,只需评论你的 class 并测试这个
一天后,我终于解决了这个问题:
@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>
我也试过这些网址
http://localhost:8080/swagger-ui/index.html
http://localhost:8080/swagger-ui/
这是我的招摇配置class。
@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>
这会起作用
@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;
}}
请使用这个 Class,只需评论你的 class 并测试这个
一天后,我终于解决了这个问题:
@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>