近来发现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. mysql 无法存储表情字符 java.sql.SQLException: Incorrect string value: '\xF0\x9F\x90\xBE",...' for column 'XXXX' at row 1

    1.变更字段类型 ALTER TABLE api_log MODIFY COLUMN remark longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_uni ...

  2. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_09 序列化流_6_练习_序列化集合

  3. Git 实践

    最近也学习了Git的相关知识,现通过一个实例来记录Git使用流程,也方便日后使用. git的基础学习: https://www.yiibai.com/git/git-quick-start.html ...

  4. UIAutomation元素识别软件

    通过Python调用UIAutomation库来开发代码时,都会遇到需要识别元素的问题.笔者在这里推荐两款好用的软件:UISpy和Inspect. UISpy识别元素后,我们需要的属性有:ClassN ...

  5. oracle--单表查询

    ---单表的查询学习 --查询表的所有数据 select * from 表名;*代表所有 select * from emp; --查询表中指定字段的值 select 字段名1,字段名2,...fro ...

  6. web 前端2 html css一些小问题技巧

    html css一些小问题技巧 1 对于儿子块float后,父亲块如果没内容就不见了,如何让父亲块依然跟随飘起了的儿子块撑起来呢?? 用到的属性after方法  公共方法作为继承即可. 1.1  方法 ...

  7. solr 安装与配置

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

  8. java_第一年_JavaWeb(15)

    Filter过滤器,Servlet API 中提供了一个Filter接口,用于实现用户在访问某个目标资源前对其进行拦截: 拦截原理:web服务器通过Filter接口调用doFilter方法,会传递一个 ...

  9. shutdown的几种方法和利弊

    1.shutdown normal      正常方式关闭数据库. 2.shutdown immediate      立即方式关闭数据库.      在SVRMGRL中执行shutdown imme ...

  10. HihoCoder - 1093 小Hi和小Ho (SPFA)

    描述 万圣节的晚上,小Hi和小Ho在吃过晚饭之后,来到了一个巨大的鬼屋! 鬼屋中一共有N个地点,分别编号为1..N,这N个地点之间互相有一些道路连通,两个地点之间可能有多条道路连通,但是并不存在一条两 ...