代码demo下载(可直接运行):https://download.csdn.net/download/yanhhuan/13102199
swagger方便生成spring restful风格的接口文档
https://blog.csdn.net/HiBoyljw/article/details/81110553?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param
使用swagger2
一、首先在pom文件内配置依赖,由于项目是spring boot聚合工程所以是写在common项目内的pom内
<!-- swagger2 配置依赖 -->
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>二、对swagger2进行配置
在Application的同级目录创建一个swagger2类
@Configuration @EnableSwagger2 public class Swagger2 { @Bean public Docket createRestApi() { List<Parameter> pars = new ArrayList<Parameter>(1); /* //swagger设置全局参数 token Parameter parameter = new ParameterBuilder().name(DtsConstants.JWT_HEADER_AUTH) .description("user token") .modelRef(new ModelRef("string")) .parameterType("header") .required(false) .build(); pars.add(parameter);*/ return new Docket(DocumentationType.SWAGGER_2) .globalOperationParameters(pars) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.restdemo")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("rest demo 接口管理(restdemo)Swagger2") .description("rest api 文档构建") .termsOfServiceUrl("http://127.0.0.1:8080/swagger-ui.html") .version("1.0") .build(); } }
三.controller
/** * 访问地址:http://localhost:8080//user/obtain/address/studentid * @Api修饰整个类,描述Controller的作用 * @Validated 在controller层开启数据校验功能 */ @RestController @RequestMapping("/user/obtain") @Api(tags ="用户 Controller") @Validated @Slf4j public class UserController { @GetMapping("/address/studentid") public String obtianUserDetail(){ log.info("start obtain user's detail-----"); return "xi'an-0801"; } }