1.增加依赖

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

2.配置类SwaggerConfig.java

package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
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; @Configuration
@EnableSwagger2
@ComponentScan(basePackages = { "com.example.demo" })
public class SwaggerConfig {
ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("demo Web service APIs")
.description("")
.license("")
.licenseUrl("")
.termsOfServiceUrl("")
.version("1.0.0")
.build();
} @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo"))
.build()
.apiInfo(apiInfo());
} }

3.启动类加 @EnableSwagger2 注解

package com.example;

import com.example.config.SupplyConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import springfox.documentation.swagger2.annotations.EnableSwagger2; @SpringBootApplication
@EnableConfigurationProperties(SupplyConfig.class)
@EnableSwagger2
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}

4.接口类加@Api及接口方法加@ApiOperation注解

package com.example.demo.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/greet")
@Api(value = "demo api")
public class HelloWorldController{ @ApiOperation(value = "问好")
@RequestMapping(value = "helloworld",method = RequestMethod.GET)
public String sayHello() {
return "hello world cc";
}
}

5.访问swagger-ui

http://localhost:8080/swagger-ui.html (端口是spring-boot项目启动的端口),打开页面如下:

发送get请求:

JavaWeb项目中集成Swagger API文档的更多相关文章

  1. 使用Swagger2构建SpringMVC项目中的Restful API文档

    使用Swagger自动生成API文档,不仅增加了项目的可维护性,还提高了API的透明度更利于快速测试等工作,便于更快地发现和解决问题. 本篇文章只记录整合过程,关于Security Configura ...

  2. 分享一个集成在项目中的REST APIs文档框架swagger

    1 为什么是使用swagger? 1-1 当后台开发人员开发好接口,是不是还要重新书写一份接口文档提给前端人员,当然对于程序员最不喜欢的就是书写文档(当然文档是必须的,有利于项目的维护) 1-2 当后 ...

  3. Swagger API文档

    Swagger API文档集中化注册管理   接口文档是前后端开发对接时很重要的一个组件.手动编写接口文档既费时,又存在文档不能随代码及时更新的问题,因此产生了像swagger这样的自动生成接口文档的 ...

  4. 添加swagger api文档到node服务

    swagger,一款api测试工具,详细介绍参考官网:http://swagger.io/ ,这里主要记录下怎么将swagger api应用到我们的node服务中: 1.任意新建node api项目, ...

  5. 基于.NetCore3.1搭建项目系列 —— 使用Swagger导出文档 (番外篇)

    前言 回顾之前的两篇Swagger做Api接口文档,我们大体上学会了如何在net core3.1的项目基础上,搭建一套自动生产API接口说明文档的框架. 本来在Swagger的基础上,前后端开发人员在 ...

  6. xadmin引入drf-yasg生成Swagger API文档

    一.安装drf-yasg: 由于django-rest-swagger已经废弃了 所以引入了drf-yasg pip install drf-yasg 安装install drf-yasg库 http ...

  7. .Net Core3.0 WebApi 项目框架搭建 二:API 文档神器 Swagger

    .Net Core3.0 WebApi 项目框架搭建:目录 为什么使用Swagger 随着互联网技术的发展,现在的网站架构基本都由原来的后端渲染,变成了:前端渲染.后端分离的形态,而且前端技术和后端技 ...

  8. Swagger API文档集中化注册管理

    接口文档是前后端开发对接时很重要的一个组件.手动编写接口文档既费时,又存在文档不能随代码及时更新的问题,因此产生了像swagger这样的自动生成接口文档的框架.swagger文档一般是随项目代码生成与 ...

  9. swagger api 文档框架

    <其他教程:https://www.cnblogs.com/FlyAway2013/p/7510279.html> 先看看swagger的生态使用图: 其中,红颜色的是swaggger官网 ...

随机推荐

  1. SqlServer 分页批按时间排序

    sql server 分页按时间排序 select * from (select<include refid="Base_Column_List"/>, ROW_NUM ...

  2. cobian backup 11 使用

    主机和备份机器创建备份用户(bf) 在备份机器上 设置备份用户, 备份目录右键安全,添加刚刚创建的备份bf用户,并授予所有权限 设置文件夹共享,并设置共享用户为刚刚创建的bf用户 并且在高级共享设置去 ...

  3. java实现打印正三角,倒三角

    正三角代码: package BasicType; /** * 封装一个可以根据用户传入值来打印正三角的方法 * @author Administrator */ public class Enme ...

  4. hashlib 实现加密

    实现代码 import hashlib # hashlib是一个python用于给数据加密的包,内有很多加密方式,包括md5,sha1, sha224, sha256, sha384, sha512等 ...

  5. [題解](最小生成樹)luogu_P1265

    首先考虑最小生成树的模型,唯一不同的是第二种情形. 即“三个或三个以上的城市申请修建的公路成环” 考虑该情形,因为修路的申请是申请离它最近的城市,所以上述条件实质上为 “存在三个或三个以上的城市,他们 ...

  6. 【aspnetcore】使用TagHelper制作分页组件

    自定义TageHelper并不难,只要记住几个点: 继承TagHelper 定义需要在TagHelper中传入的参数,如果不需要参数,可忽略 重写Process方法 在Process中拼接要输出的HT ...

  7. SNMP消息传输机制

    1.引言 基于TCP/IP的网络管理包含3个组成部分: 1) 一个管理信息库MIB(Management Information Base).管理信息库包含所有代理进程的所有可被查询和修改的参数.RF ...

  8. 转 如何快速清理 chrom 缓存

    谷歌浏览器(Chrome)如何手动清除缓存 听语音 | 浏览:13267 | 更新:2014-05-15 01:00 | 标签:谷歌 chrome 浏览器的缓存可以帮助我们更好地使用一些程序,但时间长 ...

  9. Django的filter查询

    Django的filter查询 name__contains表示精确大小写的模糊查询 使用name__icontains表示忽略大小写 year_count = DownloadFile.object ...

  10. [未读]angularjs权威教程

    正在啃,赶脚不错...