java swagger_java 集成swagger

java 集成swagger

1 引入jar

UTF-8

1.8

1.8

2.7.0

io.springfox

springfox-swagger2

${swagger.version}

io.springfox

springfox-swagger-ui

${swagger.version}

2 添加配置package com.mszl.config;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.beans.factory.annotation.Value;

import springfox.documentation.builders.ApiInfoBuilder;

import springfox.documentation.builders.PathSelectors;

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;

@EnableSwagger2

@Configuration

public class SwaggerConfig {

// 是否开启swagger,正式环境一般是需要关闭的

@Value(value = "${swagger.enabled}")

private boolean swaggerEnabled;

@Bean

public Docket createRestApi() {

return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())

.enable(swaggerEnabled)

.apiInfo(apiInfo())

.select()

.apis(RequestHandlerSelectors.basePackage("com.mszl.candidate.controller"))

.paths(PathSelectors.any())

.build();

}

private ApiInfo apiInfo() {

return new ApiInfoBuilder()

.title("候选人 API")

/*.description("bobo-teacher")

.termsOfServiceUrl("teacher")*/

.version("1.0")

.build();

}

}

3 访问:http://127.0.0.1:port/swagger-ui.html

afefb7fd2ff45efbe92db15be1e64c47.png

4 对应的注解。

@Api(tags="APP-候选人") 写在控制层上 如图4.1

@ApiOperation(value="新增身份卡")  写在方法上  如图4.2

@ApiModelProperty(value="手机号") 写在字段上 如图4.3

4.1

ae07cba5759b38dd2db06ad00020063a.png

f75b999c9e6a5e2ee0e9b29032edfc55.png

4.2

321e043a8f1ef3feeb73b48881c3a4fd.png

48807438298e3175cad77976fa5ecb49.png

4.3

19d512f65a718918d2a1b4fea97080ce.png

00e3d2d50d43fec104c79e6528e558ec.png

5 在实际中,我们在生产环境是需要关闭swagger的。我们可以通过开关去控制。

9bf174e7d2a8c0db3e6ea0a7b736c72b.png

154e034056382ff7a5179a0fedf6d29f.png

到此就完成了swagger的相关配置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值