paramType:指定参数放在哪个地方
|
header:请求参数放置于Request Header,使用@RequestHeader获取
<p>query:请求参数放置于请求地址,使用@RequestParam获取</p>
<p>path:(用于restful接口)-->请求参数的获取:@PathVariable</p>
<p>body:(不常用)</p>
<p>form(不常用)</p>
</td>
</tr><tr><td>
<p>name:参数名</p>
</td>
<td>
<p> </p>
</td>
</tr><tr><td>
<p>dataType:参数类型</p>
</td>
<td>
<p> </p>
</td>
</tr><tr><td>
<p>required:参数是否必须传</p>
</td>
<td>
<p>true | false</p>
</td>
</tr><tr><td>
<p>value:说明参数的意思</p>
</td>
<td>
<p> </p>
</td>
</tr><tr><td>
<p>defaultValue:参数的默认值</p>
</td>
<td>
<p> </p>
</td>
</tr></tbody></table></div><p><strong><em>例子:</em></strong></p>
package com.swaggerTest.controller;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
/**
* 一个用来测试swagger注解的控制器
* 注意@ApiImplicitParam的使用会影响程序运行,如果使用不当可能造成控制器收不到消息
*
* @author SUNF
*/
@Controller
@RequestMapping("/say")
@Api(value = "SayController|一个用来测试swagger注解的控制器")
public class SayController {
@ResponseBody
@RequestMapping(value ="/getUserName", method= RequestMethod.GET)
@ApiOperation(value="根据用户编号获取用户姓名", notes="test: 仅1和2有正确返回")
@ApiImplicitParam(paramType="query", name = "userNumber", value = "用户编号", required = true, dataType = "Integer")
public String getUserName(@RequestParam Integer userNumber){
if(userNumber == 1){
return "张三丰";
}
else if(userNumber == 2){
return "慕容复";
}
else{
return "未知";
}
}
@ResponseBody
@RequestMapping("/updatePassword")
@ApiOperation(value="修改用户密码", notes="根据用户id修改密码")
@ApiImplicitParams({
@ApiImplicitParam(paramType="query", name = "userId", value = "用户ID", required = true, dataType = "Integer"),
@ApiImplicitParam(paramType="query", name = "password", value = "旧密码", required = true, dataType = "String"),
@ApiImplicitParam(paramType="query", name = "newPassword", value = "新密码", required = true, dataType = "String")
})
public String updatePassword(@RequestParam(value="userId") Integer userId, @RequestParam(value="password") String password,
@RequestParam(value="newPassword") String newPassword){
if(userId <= 0 || userId > 2){
return "未知的用户";
}
if(StringUtils.isEmpty(password) || StringUtils.isEmpty(newPassword)){
return "密码不能为空";
}
if(password.equals(newPassword)){
return "新旧密码不能相同";
}
return "密码修改成功!";
}
}
完成上述代码添加上,启动Spring Boot程序,访问:http://localhost:8080/swagger-ui.html
如上图,可以看到暴漏出来的控制器信息,点击进入可以看到详细信息。
两个注意点:
1. paramType会直接影响程序的运行期,如果paramType与方法参数获取使用的注解不一致,会直接影响到参数的接收。
例如:
使用Sawgger UI进行测试,接收不到!
2. 还有一个需要注意的地方:
Conntroller中定义的方法必须在@RequestMapper中显示的指定RequestMethod类型,否则SawggerUi会默认为全类型皆可访问, API列表中会生成多条项目。
如上图:updatePassword()未指定requestMethod,结果生成了7条API信息。所以如果没有特殊需求,建议根据实际情况加上requestMethod。
5:Swagger UI面板说明
6:参考
http://blog.didispace.com/springbootswagger2/
http://blog.csdn.net/jia20003/article/details/50700736
Swagger官网 :http://swagger.io/
Spring Boot & Swagger UI : http://fruzenshtein.com/spring-boot-swagger-ui/
Github:https://github.com/swagger-api/swagger-core/wiki/Annotations
---------------------------------------------------------------------------------------
7:接收对象传参的例子
在POJO上增加
package com.zhongying.api.model.base;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
* 医生对象模型,不要使用该类
* @author SUNF
*
*/
@ApiModel(value="医生对象模型")
public class DemoDoctor{
@ApiModelProperty(value="id" ,required=true)
private Integer id;
@ApiModelProperty(value="医生姓名" ,required=true)
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 + "]";
}
}
注意: 在后台采用对象接收参数时,Swagger自带的工具采用的是JSON传参, 测试时需要在参数上加入@RequestBody,正常运行采用form或URL提交时候请删除。
package com.zhongying.api.controller.app;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageInfo;
import com.zhongying.api.exception.HttpStatus401Exception;
import com.zhongying.api.model.base.DemoDoctor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
/**
* 医生类(模拟)
* @author SUNF
*/
@RequestMapping("/api/v1")
@Controller
@Api(value = "DoctorTestController-医生信息接口模拟")
public class DoctorTestController {
/**
* 添加医生
*
* 在使用对象封装参数进行传参时,需要在该对象添加注解,将其注册到swagger中
* @link com.zhongying.api.model.base.DemoDoctor
*
* 注意: 在后台采用对象接收参数时,Swagger自带的工具采用的是JSON传参,
* 测试时需要在参数上加入@RequestBody,正常运行采用form或URL提交时候请删除。
*
* @param doctor 医生类对象
* @return
* @throws Exception
*/
@ResponseBody
@RequestMapping(value="/doctor", method= RequestMethod.POST )
@ApiOperation(value="添加医生信息", notes="")
public String addDoctor(@RequestBody DemoDoctor doctor) throws Exception{
if(null == doctor || doctor.getId() == null){
throw new HttpStatus401Exception("添加医生失败","DT3388","未知原因","请联系管理员");
}
try {
System.out.println("成功----------->"+doctor.getName());
} catch (Exception e) {
throw new HttpStatus401Exception("添加医生失败","DT3388","未知原因","请联系管理员");
}
return doctor.getId().toString();
}
/**
* 删除医生
* @param doctorId 医生ID
* @return
*/
@ResponseBody
@RequestMapping(value="/doctor/{doctorId}", method= RequestMethod.DELETE )
@ApiOperation(value="删除医生信息", notes="")
@ApiImplicitParam(paramType="query", name = "doctorId", value = "医生ID", required = true, dataType = "Integer")
public String deleteDoctor(@RequestParam Integer doctorId){
if(doctorId > 2){
return "删除失败";
}
return "删除成功";
}
/**
* 修改医生信息
* @param doctorId 医生ID
* @param doctor 医生信息
* @return
* @throws HttpStatus401Exception
*/
@ResponseBody
@RequestMapping(value="/doctor/{doctorId}", method= RequestMethod.POST )
@ApiOperation(value="修改医生信息", notes="")
@ApiImplicitParam(paramType="query", name = "doctorId", value = "医生ID", required = true, dataType = "Integer")
public String updateDoctor(@RequestParam Integer doctorId, @RequestBody DemoDoctor doctor) throws HttpStatus401Exception{
if(null == doctorId || null == doctor){
throw new HttpStatus401Exception("修改医生信息失败","DT3391","id不能为空","请修改");
}
if(doctorId > 5 ){
throw new HttpStatus401Exception("医生不存在","DT3392","错误的ID","请更换ID");
}
System.out.println(doctorId);
System.out.println(doctor);
return "修改成功";
}
/**
* 获取医生详细信息
* @param doctorId 医生ID
* @return
* @throws HttpStatus401Exception
*/
@ResponseBody
@RequestMapping(value="/doctor/{doctorId}", method= RequestMethod.GET )
@ApiOperation(value="获取医生详细信息", notes="仅返回姓名..")
@ApiImplicitParam(paramType="query", name = "doctorId", value = "医生ID", required = true, dataType = "Integer")
public DemoDoctor getDoctorDetail(@RequestParam Integer doctorId) throws HttpStatus401Exception{
System.out.println(doctorId);
if(null == doctorId){
throw new HttpStatus401Exception("查看医生信息失败","DT3390","未知原因","请联系管理员");
}
if(doctorId > 3){
throw new HttpStatus401Exception("医生不存在","DT3392","错误的ID","请更换ID");
}
DemoDoctor doctor = new DemoDoctor();
doctor.setId(1);
doctor.setName("测试员");
return doctor;
}
/**
* 获取医生列表
* @param pageIndex 当前页数
* @param pageSize 每页记录数
* @param request
* @return
* @throws HttpStatus401Exception
*/
@ResponseBody
@RequestMapping(value="/doctor", method= RequestMethod.GET )
@ApiOperation(value="获取医生列表", notes="目前一次全部取,不分页")
@ApiImplicitParams({
@ApiImplicitParam(paramType="header", name = "token", value = "token", required = true, dataType = "String"),
@ApiImplicitParam(paramType="query", name = "pageIndex", value = "当前页数", required = false, dataType = "String"),
@ApiImplicitParam(paramType="query", name = "pageSize", value = "每页记录数", required = true, dataType = "String"),
})
public PageInfo<DemoDoctor> getDoctorList(@RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex,
@RequestParam(value = "pageSize", required = false) Integer pageSize,
HttpServletRequest request) throws HttpStatus401Exception{
String token = request.getHeader("token");
if(null == token){
throw new HttpStatus401Exception("没有权限","SS8888","没有权限","请查看操作文档");
}
if(null == pageSize){
throw new HttpStatus401Exception("每页记录数不粗安在","DT3399","不存在pageSize","请查看操作文档");
}
DemoDoctor doctor1 = new DemoDoctor();
doctor1.setId(1);
doctor1.setName("测试员1");
DemoDoctor doctor2 = new DemoDoctor();
doctor2.setId(2);
doctor2.setName("测试员2");
List<DemoDoctor> doctorList = new ArrayList<DemoDoctor>();
doctorList.add(doctor1);
doctorList.add(doctor2);
return new PageInfo<DemoDoctor>(doctorList);
}
}
增加header:
现在很多请求需要在header增加额外参数,可以参考getDoctorList()的做法,使用request接收
原文地址:https://blog.csdn.net/xiaoxinxin123456789/article/details/83994859
最新文章
- coreseek (sphinx)+ Mysql + Thinkphp搭建中文搜索引擎详解
- 移动WebApp利用Chrome浏览器进行调试
- SQL Server之存储过程基础知
- JS保留小数点(四舍五入、四舍六入)实例
- WPF总结
- Cmd Markdown 简明语法手册
- UDID
- margin-top失效及解决办法
- 非常多人不愿意承认汉澳sinox已经超过windows
- CSS知识总结之设计模式(持续学习中)
- R语言机器学习之caret包运用
- 201521123040《Java程序设计》第14周学习总结
- CF1013B And
- 原生js获取元素非行内样式属性的方法
- Vue.js项目详解
- Linux 查看进程运行的完整路径方法
- win7 php nginx 启动命令
- redux笔记 进阶
- java垃圾回收的回收器
- MDK5 and STM32Cube
热门文章
- QT5.2 Assistant-设置应用程序图标
- leetcode.矩阵.240搜索二维矩阵II-Java
- 2019-8-31-How-to-use-code-to-exit-the-application-in-UWP
- 【扯淡篇】CTSC/APIO/SDOI R2时在干什么?有没有空?可以来做分母吗?
- 分布式消息中间件(二)ActiveMQ
- Java——抽象:abstract
- springCloud参考资料
- 重新开始学习C++
- lvs+keepalived+mariadb集群
- Delphi Base64编码_解码及ZLib压缩_解压(转)
|