Spring boot 2.0 -- swagger2 整合 swagger-ui.html 打不开问题

1- mvn 配置

        <!-- 接口文档 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.4.0</version>
        </dependency>

2- javaConfig

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.gm.ds.api"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("Simple APIs")
                .description("simple apis")
                .termsOfServiceUrl("http://www.gm.com")
                .contact(new Contact("suliyea", "http://xxx", "suliyea@qq.com"))
                .version("1.0")
                .build();
    }

}

3- 编写测试 Controller

@RequestMapping("/api/user")
@Api(value = "用户接口", description = "")
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @RequestMapping(value = "login", method = RequestMethod.GET)
    @ResponseBody
    public ApiData login(@RequestParam(value = "username", defaultValue = "") String username,
                         @RequestParam(value = "password", defaultValue = "") String password) {
        //TODO....
        return new ApiData().setMessage("用户帐号不存在!").setStatus(ApiData.ERROR);
    }
}

4- 按常规, 现在访问http://localhost:8090/swagger-ui.html 应该能看到效果了, 可是访问页面出现这样提示

Paste_Image.png

5- 处理方法

在 GitHub 上下载 SwaggerUI 项目(https://github.com/swagger-api/swagger-ui
),将dist下所有内容拷贝到本地项目resource/static/swagger下面, 并修改 index.html

//url = "http://petstore.swagger.io/v2/swagger.json";        
url = "http://localhost:8090/v2/api-docs";

这里的url是死的。

然后访问http://localhost:8090/swagger/index.html



作者:雪中洛阳
链接:https://www.jianshu.com/p/cbb6d89b88d8
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值