Swagger请求的更改请求的url地址
前言1、引入swagger 相关jar包<!--swagger 版本--><swagger.version>2.7.0</swagger.version><!--swagger--><dependency><groupId>io.s
·
前言
1、引入swagger 相关jar包
<!--swagger 版本-->
<swagger.version>2.7.0</swagger.version>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
2、选择一个swagger的目录下载
必须选择2.0以上,3.0以下版本,将其中的dist文件夹拷贝到自己项目中的resources/swagger目录下,如图
https://github.com/swagger-api/swagger-ui

3、在resources下新建swagger.properties文件,其中的内容为
springfox.documentation.swagger.v2.path=/duodian/swagger
4、再dist目录下面的index.html中添加
url = "http://petstore.swagger.io/v2/swagger.json"
修改为
url = url = "/duodian/swagger";
修改后如下
<script type="text/javascript">
$(function () {
var url = window.location.search.match(/url=([^&]+)/);
if (url && url.length > 1) {
url = decodeURIComponent(url[1]);
} else {
url = "/duodian/swagger";
}
5、新建swag配置文件
@EnableSwagger2
@Configuration
@PropertySource("classpath:swagger.properties") // 新增对swagger.properties 的引入
public class ApiConfig extends WebMvcConfigurerAdapter{
@Profile({"test","dev"})
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com"))
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("多点优惠")
.description("多点优惠开发文档")
.version("1.0.0")
.termsOfServiceUrl("http://test.dangqugame.cn/")
.license("dangqugame")
.licenseUrl("http://test.dangqugame.cn/")
.build();
}
}
6、添加资源的映射
@Configuration
public class InterceptorConfig extends WebMvcConfigurerAdapter{
/**
* swagger增加url映射
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/duodian/swagger/**").addResourceLocations("classpath:/swagger/dist/");
}
}
7、访问成功
http://localhost:8080/duodian/swagger/index.html


「智能机器人开发者大赛」官方平台,致力于为开发者和参赛选手提供赛事技术指导、行业标准解读及团队实战案例解析;聚焦智能机器人开发全栈技术闭环,助力开发者攻克技术瓶颈,促进软硬件集成、场景应用及商业化落地的深度研讨。 加入智能机器人开发者社区iRobot Developer,与全球极客并肩突破技术边界,定义机器人开发的未来范式!
更多推荐



所有评论(0)