首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swagger3 3-ui URL在我的Spring项目中不起作用

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

Stack Overflow用户
提问于 2021-02-17 18:14:22
回答 3查看 589关注 0票数 0

我也试过这些urls

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

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

这是我的招摇配置类。

代码语言:javascript
复制
@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

代码语言:javascript
复制
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-02-17 20:05:40

最后,过了一天,我就这样修好了:

代码语言:javascript
复制
@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);
    }

}
代码语言:javascript
复制
    <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>
票数 0
EN

Stack Overflow用户

发布于 2021-02-17 18:24:57

正确的url是

代码语言:javascript
复制
http://localhost:8080/swagger-ui.html

添加此注释

代码语言:javascript
复制
@EnableSwagger2

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

代码语言:javascript
复制
    <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>

这会起作用的

票数 0
EN

Stack Overflow用户

发布于 2021-02-17 18:48:36

代码语言:javascript
复制
@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;
}}

请使用这个类,只需注释您的类,并测试此

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66247672

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档