「小程序JAVA实战」swagger2的使用与接口测试(34)
转自:https://idig8.com/2018/08/31/xiaochengxujavashizhanswagger2deshiyongyujiekouceshi34/
我们已经开发完了一个用户注册的接口,但是我们并没有测试也不知道里面哪里忽略了,有什么问题,先讲下下swagger2,然后集成到spring boot这个项目中。源码:https://github.com/limingios/wxProgram.git 中的wx-springboot
swagger2
- 介绍
>swagger2 是可以构建一个非常强大的,是个非常好用的工具也是个非常好用的插件。
- 可以生成文档形式的api并提供给不同的团队
- 便于自测,也便于领导查阅任务量。
- 无需过多冗余的word文档。保证文档是最新的。
- 使用方法
>在spring boot common中pom中引入

<!-- swagger2 配置 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
- 添加swagger2的配置文件
>在spring boot api中加入java类

package com.idig8;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2 {
/**
* @Description:swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
*/
@Bean
public Docket createRestApi() {
// 为swagger添加header参数可供输入
ParameterBuilder userTokenHeader = new ParameterBuilder();
ParameterBuilder userIdHeader = new ParameterBuilder();
List<Parameter> pars = new ArrayList<Parameter>();
userTokenHeader.name("headerUserToken").description("userToken")
.modelRef(new ModelRef("string")).parameterType("header")
.required(false).build();
userIdHeader.name("headerUserId").description("userId")
.modelRef(new ModelRef("string")).parameterType("header")
.required(false).build();
pars.add(userTokenHeader.build());
pars.add(userIdHeader.build());
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage("com.idig8.controller"))
.paths(PathSelectors.any()).build()
.globalOperationParameters(pars);
}
/**
* @Description: 构建 api文档的信息
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 设置页面标题
.title("使用swagger2构建小程序后端api接口文档")
// 设置联系人
.contact(new Contact("IT人故事会", "https://idig8.com", "公众号:编程坑太多"))
// 描述
.description("欢迎访问接口文档")
// 定义版本号
.version("1.0").build();
}
}
- 修改原有的controller的修改
package com.idig8.controller;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.util.StringUtil;
import com.idig8.pojo.Users;
import com.idig8.service.UserService;
import com.idig8.utils.JSONResult;
import com.idig8.utils.MD5Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@RestController
@Api(value="用户注册登录的接口",tags={"注册和登录的controller"})
public class RegistLoginController {
@Autowired
private UserService userService;
@ApiOperation(value="用户注册",notes="用户注册的接口")
@PostMapping("/regist")
public JSONResult regist(@RequestBody Users user) {
//1.判断用户名和密码不能为空
if(StringUtils.isBlank(user.getUsername())||StringUtils.isBlank(user.getPassword())) {
return JSONResult.errorMsg("用户名或密码不能为空");
}
//2.判断用户名是否存在
boolean usernameIsExist = userService.queryUsernameIsExist(user.getUsername());
if(!usernameIsExist) {
user.setNickname(user.getUsername());
try {
user.setPassword(MD5Utils.getMD5Str(user.getPassword()));
} catch (Exception e) {
return JSONResult.errorMsg(e.getMessage());
}
user.setFollowCounts(0);
user.setReceiveLikeCounts(0);
user.setFansCounts(0);
userService.saveUser(user);
}else {
return JSONResult.errorMsg("用户名或已经存在,请更换在试试!");
}
return JSONResult.ok();
}
}
- 增加Users属性的限制,那些必填 ,spring-boot pojo项目
package com.idig8.pojo;
import javax.persistence.*;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel(value="用户对象",description="这是用户对象")
public class Users {
@Id
@ApiModelProperty(hidden=true)
private String id;
/**
* 用户名
*/
@ApiModelProperty(value="用户名",name="username",example="idig8",required=true)
private String username;
/**
* 密码
*/
@ApiModelProperty(value="密码",name="password",example="123456",required=true)
private String password;
/**
* 我的头像,如果没有默认给一张
*/
@Column(name = "face_image")
private String faceImage;
/**
* 昵称
*/
@ApiModelProperty(hidden=true)
private String nickname;
/**
* 我的粉丝数量
*/
@ApiModelProperty(hidden=true)
@Column(name = "fans_counts")
private Integer fansCounts;
/**
* 我关注的人总数
*/
@ApiModelProperty(hidden=true)
@Column(name = "follow_counts")
private Integer followCounts;
/**
* 我接受到的赞美/收藏 的数量
*/
@ApiModelProperty(hidden=true)
@Column(name = "receive_like_counts")
private Integer receiveLikeCounts;
/**
* @return id
*/
public String getId() {
return id;
}
/**
* @param id
*/
public void setId(String id) {
this.id = id;
}
/**
* 获取用户名
*
* @return username - 用户名
*/
public String getUsername() {
return username;
}
/**
* 设置用户名
*
* @param username 用户名
*/
public void setUsername(String username) {
this.username = username;
}
/**
* 获取密码
*
* @return password - 密码
*/
public String getPassword() {
return password;
}
/**
* 设置密码
*
* @param password 密码
*/
public void setPassword(String password) {
this.password = password;
}
/**
* 获取我的头像,如果没有默认给一张
*
* @return face_image - 我的头像,如果没有默认给一张
*/
public String getFaceImage() {
return faceImage;
}
/**
* 设置我的头像,如果没有默认给一张
*
* @param faceImage 我的头像,如果没有默认给一张
*/
public void setFaceImage(String faceImage) {
this.faceImage = faceImage;
}
/**
* 获取昵称
*
* @return nickname - 昵称
*/
public String getNickname() {
return nickname;
}
/**
* 设置昵称
*
* @param nickname 昵称
*/
public void setNickname(String nickname) {
this.nickname = nickname;
}
/**
* 获取我的粉丝数量
*
* @return fans_counts - 我的粉丝数量
*/
public Integer getFansCounts() {
return fansCounts;
}
/**
* 设置我的粉丝数量
*
* @param fansCounts 我的粉丝数量
*/
public void setFansCounts(Integer fansCounts) {
this.fansCounts = fansCounts;
}
/**
* 获取我关注的人总数
*
* @return follow_counts - 我关注的人总数
*/
public Integer getFollowCounts() {
return followCounts;
}
/**
* 设置我关注的人总数
*
* @param followCounts 我关注的人总数
*/
public void setFollowCounts(Integer followCounts) {
this.followCounts = followCounts;
}
/**
* 获取我接受到的赞美/收藏 的数量
*
* @return receive_like_counts - 我接受到的赞美/收藏 的数量
*/
public Integer getReceiveLikeCounts() {
return receiveLikeCounts;
}
/**
* 设置我接受到的赞美/收藏 的数量
*
* @param receiveLikeCounts 我接受到的赞美/收藏 的数量
*/
public void setReceiveLikeCounts(Integer receiveLikeCounts) {
this.receiveLikeCounts = receiveLikeCounts;
}
}
运行项目
- 报错:没有增加扫描
Action:
Consider defining a bean of type 'com.idig8.mapper.UsersMapper' in your configuration.
增加id自动生成Sid的扫描,在service里面注入了和mapper的扫描
··· java
package com.idig8;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan(basePackages=”com.idig8.mapper”)
@ComponentScan(basePackages= {“com.idig8″,”org.n3r.idworker”})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
···
- 访问网址http://localhost:8081/swagger-ui.html

点击左边的实例,修改内容,点击try it out!

返回的结果

数据库插入正常

- 里面的内容设置成空


- 用户名设置成idig8 在试试接口


PS:到这里swagger2的文档配置和接口配置,包括错误的的接口测试基本都演示完毕了。
「小程序JAVA实战」swagger2的使用与接口测试(34)的更多相关文章
- 「小程序JAVA实战」springboot的后台搭建(31)
转自:https://idig8.com/2018/08/29/xiaochengxujavashizhanspringbootdehoutaidajian31/ 根据下面的图,我们来建立下对应的sp ...
- 「小程序JAVA实战」微信小程序简介(一)
转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-01/ 一直想学习小程序,苦于比较忙,加班比较多没时间,其实这都是理由,很多时候习惯了搬砖,习惯了固 ...
- 「小程序JAVA实战」微信小程序工程结构了解(五)
转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-05/ 微信小程序工程结构 audio,button,canvas,checkbox 都是由4个文件 ...
- 「小程序JAVA实战」小程序的springboot后台拦截器(61)
转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxudespringboothoutailanjieqi60/ 之前咱们把 ...
- 「小程序JAVA实战」小程序头像图片上传(下)(45)
转自:https://idig8.com/2018/09/09/xiaochengxujavashizhanxiaochengxutouxiangtupianshangchuan44/ 接下来,我们应 ...
- 「小程序JAVA实战」小程序的留言和评价功能(70)
转自:https://idig8.com/2018/10/28/xiaochengxujavashizhanxiaochengxudeliuyanhepingjiagongneng69/ 目前小程序这 ...
- 「小程序JAVA实战」小程序的举报功能开发(68)
转自:https://idig8.com/2018/09/25/xiaochengxujavashizhanxiaochengxudeweixinapicaidancaozuo66-2/ 通过点击举报 ...
- 「小程序JAVA实战」小程序的个人信息作品,收藏,关注(66)
转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxudegerenxinxizuopinshoucangguanzhu65 ...
- 「小程序JAVA实战」小程序的关注功能(65)
转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxudeguanzhugongneng64/ 在个人页面,根据发布者个人和 ...
随机推荐
- TCP/IP 必知必会的十个问题
本文整理了一些TCP/IP协议簇中需要必知必会的十大问题,既是面试高频问题,又是程序员必备基础素养. 一.TCP/IP模型 TCP/IP协议模型(Transmission Control Protoc ...
- 特性属性 @property
实现其它语言所拥有的 getter 和 setter 的功能 作用: 用来模拟一个属性 通过@property 装饰器可以对模拟属性的取值和赋值加以控制 class Student: def __in ...
- Android中的按键顺序打乱
首先要找到相对应的控件,之后再来一个数组,再把数组里面的数字显示到控件上面. private void initView() { mNum0 = (Button) findViewById(R.id. ...
- java project 项目在 linux 下面部署方法
1.前提是安装好了响应的开发和部署环境,例如jdk. 2.在Linux下运行可执行Jar包,首先准备jar包,一般的编译工具Eclipse,jbuilder都提供export功能,可以生成jar包. ...
- java基础:eclipse编程不得不知道的技巧
如果你是位具有开发经丰富的工程师,在开发的过程中,你就会很强烈的要求快捷的编程.如何快捷编程,只有更加熟悉开发工具.那么eclipse是同样也有很多技巧.可以带着下面问题来阅读1.如何查找类相关信息? ...
- 零基础学习hadoop到上手工作线路指导初级篇:hive及mapreduce
此篇是在零基础学习hadoop到上手工作线路指导(初级篇)的基础,一个继续总结.五一假期:在写点内容,也算是总结.上面我们会了基本的编程,我们需要对hadoop有一个更深的理解:hadoop分为h ...
- Ubuntu下环境变量设置
[内容来自网络] 相应配置文件介绍: 1) /etc/profile :在登录时,操作系统定制用户环境使用的第一个文件,此文件为系统的每个用户设置环境信息,当用户第一次登录时,改文件被执行 2) /e ...
- MySQL-Proxy 读写分离、同步延时问题解决方案
MySQL-Proxy 读写分离.同步延时问题解决方案 使用MySQL将读写请求转接到主从Server. 一 安装MySQL Proxy MySQL Proxy的二进制版非常方便,下载解压缩后即用. ...
- grpc gateway 使用以及docker compose 集成
1. grpc gateway 安装 参考,比较简单,有需要的依赖可以参考相资料 mkdir tmp cd tmp git clone https://github.com/google/protob ...
- mysql存储引擎之myisam学习
myisam存储引擎特点:1.不支持事务2.表级锁定(更新时锁整个表,其索引机制是表级索引,这虽然可以让锁定的实现成本很小,但是也同时大大降低 了其并发性能) 3.读写互相阻塞:不仅会在写入的时候阻塞 ...
