SpringMVC + swagger修改swagger-ui.html的访问路径

文档:SpringMVC集成SwaggerUI修改访问路径
链接:http://note.youdao.com/noteshare?id=752e0c4aa08400155a885965d05dd78b&sub=7842F4CB0FF142069D2101920134586A 

需求

springmvc 加入 swagger 依赖:

    //接入swagger
    compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
    compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'

屋里知道,集成成功以后,访问 http://xxx:8080/swagger-ui.html 就可以了。

如果想要将访问地址更改成:http://xxx:8080/api/swagger/swagger-ui.html

该怎么办呢?

 

思路

swagger-ui.jar包下面,存放着html和js等文件。

项目启动后,访问 http://xxx:8080/ 可以看到,这些文件会存放在webroot目录下面,和WEB-INF同级

  

解决

1、swagger的配置和使用就省略啦

2、在mvc的配置文件中增加以下配置:

<bean class="com.hfy.config.Swagger2Config" />
<mvc:resources mapping="api/swagger/**" location="classpath:/META-INF/resources/"/>

3、增加一个controller,对一些请求进行转发

@Controller
public class SwaggerController {

    @RequestMapping("/api/swagger/swagger-resources")
    public String resource() {
        return "forward:/swagger-resources";
    }

    @RequestMapping("/api/swagger/swagger-resources/configuration/ui")
    public String ui() {
        return "forward:/swagger-resources/configuration/ui";
    }

    @RequestMapping("/api/swagger/v2/api-docs")
    public String doc() {
        return "forward:/v2/api-docs";
    }
    @RequestMapping("/api/swagger/swagger-resources/configuration/security")
    public String security() {
        return "forward:/swagger-resources/configuration/security";
    }
}

额外:如果想屏蔽之前的 http://xxx:8080/swagger-ui.html 链接,可以 写个方法,response中设置状态码404。

好了,؏؏☝ᖗ乛◡乛ᖘ☝؏؏

参考

https://github.com/springfox/springfox/issues/1080

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值