swagger是一个功能强大的api框架,它的集成非常简单,不仅提供了在线文档的查阅,而且还提供了在线文档的测试。

(1) 引入依赖,我们选择现在最新的版本

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency> <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>

(2) 写配置类

/**
* @Auther: curry
* @Date: 2018/6/3 12:46
* @Description:
*/
@Configuration
@EnableSwagger2
public class SwaggerProperties {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.imooc.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot利用swagger构建api文档")
.description("")
.termsOfServiceUrl("")
.version("1.0")
.build();
}
}

(3) 在controller 中进行引入注解(当然也可以不写)


/**
* @Auther: curry
* @Date: 2018/5/28 21:57
* @Description:
*/
@RestController
public class GirlController {
private final static Logger logger = LoggerFactory.getLogger(GirlController.class);
@Resource
private GirlRepository girlRepository; @Resource
private GirlService girlService; @ApiOperation(value="获取女孩列表", notes="获取女孩列表")
@GetMapping("/girls")
public List<Girl> getList(){
logger.info("getList");
return girlRepository.findAll();
} @ApiOperation(value = "添加女孩" ,notes="添加女孩")
@PostMapping("/girls")
public Result<Girl> girlAdd(@Valid Girl girl, BindingResult bindingResult){
if(bindingResult.hasErrors()){
return ResultUtil.error(1,bindingResult.getFieldError().getDefaultMessage());
}
return ResultUtil.success(girlRepository.save(girl)); } @ApiOperation(value = "查找女孩",notes = "查找女孩")
@ApiImplicitParam(name = "id" ,value ="查找女孩" ,required = true,dataType = "int",paramType = "path")
@GetMapping(value = "/girls/{id}")
public Girl find(@PathVariable(value = "id") Integer id){
return girlRepository.findById(id).get();
} @ApiOperation(value = "修改女孩",notes = "根据id查找女孩并修改")
@PostMapping(value = "/girls/{id}")
public Girl update(@PathVariable(value = "id") Integer id,
@RequestParam("name") String name,
@RequestParam("age") int age){
Girl girl = new Girl();
girl.setId(id);
girl.setAge(age);
girl.setName(name);
return girlRepository.save(girl); } @ApiOperation(value = "删除女孩",notes = "根据id删除女孩")
@DeleteMapping(value = "/girls/{id}")
public void delete(@PathVariable(value = "id") Integer id){
girlRepository.deleteById(id);
} @ApiOperation(value = "根据年龄查询女孩")
@GetMapping(value = "/girls/age/{age}")
public List<Girl> findByAge(@PathVariable(value = "age") Integer age){
return girlRepository.findByAge(age);
} @GetMapping(value = "/girls/getAge/{id}")
public void getAge(@PathVariable("id") Integer id) throws Exception {
girlService.getAge(id);
}
}

(4) 访问http://localhost:8099/swagger-ui.html



(5)当然,你也可以不在controller 中增加这个注解,是不是感觉很方便,很能测试,just do it!

SpringBoot(八)_springboot集成swagger2的更多相关文章

  1. SpringBoot(十)_springboot集成Redis

    Redis 介绍 Redis是一款开源的使用ANSI C语言编写.遵守BSD协议.支持网络.可基于内存也可持久化的日志型.Key-Value高性能数据库. 数据模型 Redis 数据模型不仅与关系数据 ...

  2. SpringBoot(九)_springboot集成 MyBatis

    MyBatis 是一款标准的 ORM 框架,被广泛的应用于各企业开发中.具体细节这里就不在叙述,大家自行查找资料进行学习下. 加载依赖 <dependency> <groupId&g ...

  3. SpringBoot集成Swagger2在线文档

    目录 SpringBoot集成Swagger2在线文档 前言 集成SpringBoot 登录接口文档示例 代码 效果 注解说明 总结 SpringBoot集成Swagger2在线文档 前言 不得不说, ...

  4. 【java框架】SpringBoot(3) -- SpringBoot集成Swagger2

    1.SpringBoot web项目集成Swagger2 1.1.认识Swagger2 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体 ...

  5. SpringBoot集成Swagger2实现Restful(类型转换错误解决办法)

    1.pom.xml增加依赖包 <dependency> <groupId>io.springfox</groupId> <artifactId>spri ...

  6. springboot集成swagger2构建RESTful API文档

    在开发过程中,有时候我们需要不停的测试接口,自测,或者交由测试测试接口,我们需要构建一个文档,都是单独写,太麻烦了,现在使用springboot集成swagger2来构建RESTful API文档,可 ...

  7. springboot 集成swagger2

    使用Swagger 可以动态生成Api接口文档,在项目开发过程中可以帮助前端开发同事减少和后端同事的沟通成本,而是直接参照生成的API接口文档进行开发,提高了开发效率.这里以springboot(版本 ...

  8. springboot 集成swagger2.x 后静态资源报404

    package com.bgs360.configuration; import org.springframework.context.EnvironmentAware; import org.sp ...

  9. SpringBoot 集成Swagger2自动生成文档和导出成静态文件

    目录 1. 简介 2. 集成Swagger2 2.1 导入Swagger库 2.2 配置Swagger基本信息 2.3 使用Swagger注解 2.4 文档效果图 3. 常用注解介绍 4. Swagg ...

随机推荐

  1. Ubuntu环境下安装CUDA9.0

    前言: 本篇文章是基于安装CUDA 9.0的经验写,CUDA9.0目前支持Ubuntu16.04和Ubuntu17.04两个版本,如下图所示(最下面的安装方式我们选择第一个,即runfile方式): ...

  2. P4774 [NOI2018]屠龙勇士

    P4774 [NOI2018]屠龙勇士 先平衡树跑出打每条龙的atk t[] 然后每条龙有\(xt \equiv a[i](\text{mod }p[i])\) 就是\(xt+kp[i]=a[i]\) ...

  3. idea web项目debug模式实时更新按钮不生效原因

    必须两个都开启才能生效,单按按钮不能生效,但是有时候自动更新不生效的时候按按钮后可以生效, 如果前端目录或后端内容实在不更新,就删掉out目录和target目录,重新启动服务器即可

  4. 为什么java代码中要避免多层深度for循环嵌套

    在开发中,一直强调代码的整洁和可读性.之前对于使用多层嵌套for循环,一直以为只是对代码可读性和逻辑梳理有影响.可能对性能也有影响,但是一直不知道对性能影响在哪.最近在看虚拟机方面的书,感觉有一个点应 ...

  5. Permission Policies

    The Permission Policy determines Security System behavior when there are no explicitly specified per ...

  6. AssetBundle一些问题

    AssetBundle划分过细的问题,比如每个资源都是AssetBundle. 加载IO次数过多,从而增大了硬件设备耗能和发热的压力: Unity 5.3 ~ 5.5 版本中,Android平台上在不 ...

  7. 402. Remove K Digits/738.Monotone Increasing Digits/321. Create Maximum Number

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  8. 环境变量的配置-java-JMETER - 【Linux】

    rz上传 lz下载 步骤: . Linux下首先安装Jdk: . 下载apache-jmeter-4.0.tgz,复制到Linux系统中的/opt目录下: . 解压apache-jmeter-4.0. ...

  9. 前端开发利器 livereload -- 从此告别浏览器F5键

    各位从事前端开发的童鞋们,大家每天coding && coding,然后F5 && F5,今天推荐一个静态文件在浏览器中自动更新的扩展 livereload,不同手动刷 ...

  10. Flink架构分析之HA

    抽象 LeaderElectionService 这个接口用于从一组竞选者中选出一个leader,其start方法需要传递一个LeaderContender竞选者作为参数,如果有多个竞选者,则每一个竞 ...