简体   繁体   English

带有swagger和Spring MVC的Rest Api文档

[英]Rest Api documentation with swagger and Spring MVC

Hi' I have several REST api in my spring mvc project and I would like to create documentation for each service and store it in file so I can share with the team. 嗨,我在spring mvc项目中有几个REST api,我想为每个服务创建文档并将其存储在文件中,以便与团队共享。 I often read about Swagger or Springfox and configure it so, I added 我经常阅读有关Swagger或Springfox并对其进行配置的信息,因此我补充道

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.2.2</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.7.0-rc1</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.1.2</version>
</dependency>

and config class: 和配置类:

package com.config.core;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import static springfox.documentation.builders.PathSelectors.*;
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(regex("/api/.*"))
            .build()
            .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfo(
            "My Project's REST API",
            "1.0",
            "This is a description of your API.",
            "API TOS",
            "me@wherever.com",
            "API License",
            "API License URL"
        );
        return apiInfo;
    }
}

but swagger-ui.html doesn't exist, I have : 但是swagger-ui.html不存在,我有:

> /v2/api-docs 
> /configuration/security
> /configuration/ui 
> /swagger-resources

So, how can I see my web server documentation and save it on file? 那么,如何查看我的Web服务器文档并将其保存在文件中? I would like to avoid to show on url when users have to use the web platform.Thanks 我想避免在用户必须使用Web平台时在url上显示。谢谢

springfox-swagger-ui is a webjar and the resources embedded in the jar need to be registered via resource handlers. springfox-swagger-ui是一个webjar,需要通过资源处理程序注册jar中嵌入的资源。

You need to add the resource handlers as demonstrated in the spring mvc demo . 您需要按照spring mvc演示中的说明添加资源处理程序。 That way the swagger ui assets can be rendered by the application. 这样,可通过应用程序呈现招摇的ui资产。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM