近来发现knife4j比swagger2整合起来方便,功能也更强大,推荐使用, 具体可参考 springboot2整合knife4j

1.目的:使用Swagger2发布接口,ui可操作

2.项目结构

 3. 代码

3.1 接口类qinfeng.zheng.api.controller.DemoController

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import qinfeng.zheng.api.entity.UserEntity;
@Api(value = "会员接口")
@RestController
public class DemoController { @ApiOperation(value = "swagger接口测试demo", nickname = "swagger接口测试demo昵称")
@GetMapping("/getDemo")
public String getDemo() {
return "getDemo方法调用成功...";
} @ApiOperation(value = "获取会员信息接口", nickname = "根据userName获取用户相关信息")
@ApiImplicitParam(name = "userName", value = "用户名称", required = true, dataType = "String")
@PostMapping("/postMember")
public String postMember(@RequestParam String userName) {
return userName;
} @ApiOperation(value = "添加用户信息", nickname = "nickname是什么", notes = "notes是什么", produces = "application/json")
@PostMapping("/postUser")
@ResponseBody
@ApiImplicitParam(paramType = "query", name = "userId", value = "用户id", required = true, dataType = "int")
public UserEntity postUser(@RequestBody UserEntity user, @RequestParam("userId") int userId) { // 这里用包装类竟然报错
if (user.getId() == userId) {
return user;
}
return new UserEntity();
} @ApiOperation(value = "添加用户信息", nickname = "哈哈测试", notes = "哈哈测试添加用户", produces = "application/json")
@PostMapping("/addUser")
@ResponseBody
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "userName", value = "用户姓名", required = true, dataType = "String"),
@ApiImplicitParam(paramType = "query", name = "id", value = "用户id", required = true, dataType = "int") })
public UserEntity addUser(String userName, int id) {
UserEntity userEntity = new UserEntity();
userEntity.setName(userName);
userEntity.setId(id);
return userEntity;
} }

  

  3.2 实体类qinfeng.zheng.api.entity.UserEntity

package qinfeng.zheng.api.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; /**
* 创建时间: 23:09 2018/9/19
* 修改时间:
* 编码人员: ZhengQf
* 版 本: 0.0.1
* 功能描述:
*/
@ApiModel(value = "用户模型")
public class UserEntity {
@ApiModelProperty(value="id" ,required= true,example = "123")
private Integer id;
@ApiModelProperty(value="用户姓名" ,required=true,example = "郑钦锋")
private String name; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "DemoDoctor [id=" + id + ", name=" + name + "]";
} }

  3.3 配置类qinfeng.zheng.config.SwaggerConfig

package qinfeng.zheng.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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; /**
* swagger2的配置类
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig { @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
// api扫包范围
.apis(RequestHandlerSelectors.basePackage("qinfeng.zheng.api")).paths(PathSelectors.any()).build();
} /**
* 创建该API的基本信息(这些基本信息会展现在文档页面中)
* 访问地址:http://项目实际地址/swagger-ui.html
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("Swagger接口发布测试").description("测试|Swagger接口功能")
.termsOfServiceUrl("http://www.baidu.com")
.version("1.0").build();
} }

  3.4 启动类qinfeng.zheng.AppSwagger

package qinfeng.zheng;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class AppSwagger {
public static void main(String[] args) {
SpringApplication.run(AppSwagger.class, args);
}
}

  3.5 application.yml

server:
port: 8080
spring:
application:
name: swagger

  3.6 maven依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>qinfeng.zheng</groupId>
<artifactId>springboot-swagger-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>springboot-swagger-demo</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-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>
</dependencies>
</project>

4. 启动项目

  4.1 项目启动成功之后,浏览器访问http://localhost:8080/swagger-ui.html

  

  4.2 测试addUser接口

  

 点击Execute提交请求,

  请求成功,其它接口可自行测试,皆正常!!!

swagger2接口发布demo的更多相关文章

  1. WebService—CXF—实现接口发布和客户端调用

    (一)接口发布的几种方式 定义接口: @WebService(targetNamespace="http://www.itfad.net/queryUser") public in ...

  2. Asp.Net Core基于JWT认证的数据接口网关Demo

    近日,应一位朋友的邀请写了个Asp.Net Core基于JWT认证的数据接口网关Demo.朋友自己开了个公司,接到的一个升级项目,客户要求用Aps.Net Core做数据网关服务且基于JWT认证实现对 ...

  3. 支付宝即时到账接口开发 - DEMO讲解

    支付宝即时到帐接口 环境要求 PHP5.0以上,且需要开启curl.openssl. 文档地址: https://doc.open.alipay.com/doc2/detail?treeId=62&a ...

  4. WebService—CXF整合Spring实现接口发布和调用过程

    一.CXF整合Spring实现接口发布 发布过程如下: 1.引入jar包(基于maven管理) <!-- cxf --> <dependency> <groupId> ...

  5. 新手入门贴之基于 python 语言的接口自动化 demo 小实战

    大家好,我是正在学习接口测试的菜鸟.近期通过自己的学习,完成了一个关于测试接口的接口自动化demo.下面想跟大家分享一下,主要的思路是根据接口文档确定测试用例,并将测试用例写在excel中.因为只是小 ...

  6. (五)RabbitMQ消息队列-安装amqp扩展并订阅/发布Demo(PHP版)

    原文:(五)RabbitMQ消息队列-安装amqp扩展并订阅/发布Demo(PHP版) 本文将介绍在PHP中如何使用RabbitMQ来实现消息的订阅和发布.我使用的系统依然是Centos7,为了方便, ...

  7. 使用JDK自带功能,实现一个简单的Web Service接口发布

    万事开头难,本篇文章的目的就是使用JDK自带的功能,实现一个最简单的Web Service接口的发布. 下图是项目的组成,主要有三个部分,一个接口(WS),一个接口的实现类(WSImp),还有一个接口 ...

  8. 易飞ERP API接口调用DEMO

    一.使用场景: 1.需要开放ERP数据给第三方系统对接,如APP手机端开发,MES,OA等: 2.接口按现在主流开发,restful风格,传JSON数据,跨平台,不限开发工具: 3.不限易飞ERP,支 ...

  9. swagger2 接口文档

    1,maven: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www. ...

随机推荐

  1. 圆角Panel

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostic ...

  2. HttpClient实现通过url下载文件

    其实就是通过浏览器url,点击就会下载文件. 这里是从代码层面上,对文件进行下载. package main.java.com.abp.util; import org.apache.http.*; ...

  3. Python 爬取淘宝商品数据挖掘分析实战

    Python 爬取淘宝商品数据挖掘分析实战 项目内容 本案例选择>> 商品类目:沙发: 数量:共100页  4400个商品: 筛选条件:天猫.销量从高到低.价格500元以上. 爬取淘宝商品 ...

  4. 2017埙箫简谱清单分享(附音频Demo)

    前言 习箫五载,略有所获,皆在坚持. 本博文记录旨在记录练习过程中所录制的Demo以供自省.自娱.自乐,同时记录.分享简谱与箫友(目前为简谱,日后学会线谱后会添加相应谱子分类). 简谱 &&a ...

  5. SQL语句创建函数

    ----先create,再alter alter function fuc (@userid int,@strWhere varchar(max),@strWhere2 varchar(max) )  ...

  6. JAVA调用shell脚本利用ansible修改多节点上的redis参数

    创建hosts文件 创建ansible-playbook执行时所用到的hosts文件,例如 /etc/redis/hosts 利用shell命令根据传入的host名和地址写入hosts文件: #set ...

  7. JavaScript 的继承与多态

    本文先对es6发布之前javascript各种继承实现方式进行深入的分析比较,然后再介绍es6中对类继承的支持以及优缺点讨论.最后介绍了javascript面向对象编程中很少被涉及的“多态”,并提供了 ...

  8. 厉害了,Apache架构师们遵循的 30 条设计原则

    作者:Srinath 翻译:贺卓凡,来源:公众号ImportSource Srinath通过不懈的努力最终总结出了30条架构原则,他主张架构师的角色应该由开发团队本身去扮演,而不是专门有个架构师团队或 ...

  9. solr 安装与配置

    1. Solr安装与配置 1.1什么是Solr 大多数搜索引擎应用都必须具有某种搜索功能,问题是搜索功能往往是巨大的资源消耗并且它们由于沉重的数据库加载而拖垮你的应用的性能. 这就是为什么转移负载到一 ...

  10. 【洛谷p3958】奶酪

    奶酪[题目链接] 题前废话不知道说啥了啊qwq(越来越沉默寡言) 好了看题: SOLUTION: 思路的话,大概是搜索,然后大概广搜??? 但是我们今天写深搜(也是听xcg大佬讲了以后的整理博) 首先 ...