Swagger3UIURL在我的SpringBoot项目中不起作用

l5tcr1uw  于 2021-07-23  发布在  Java
关注(0)|答案(3)|浏览(412)

我也尝试过这些网址
http://localhost:8080/swagger ui/index.html
http://localhost:8080/招摇用户界面/
这是我的招摇配置类。

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

muk1a3rh1#

正确的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>

这会有用的

olqngx59

olqngx592#

@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;
}}

请使用这个类,只需注解你的类并测试这个

h22fl7wq

h22fl7wq3#

一天之后,我终于把它修好了:

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

相关问题