Java后端开发——添加Swagger语句后的Controller层代码示例
//接口定义在Controller层,接口实现在Service层
package com.xolo.core.controller; //包名 import com.xolo.core.entity.Wechat; //实体名 import com.xolo.core.request.WechatRequest; //请求类型 import com.xolo.core.response.Response; //返回值类型 import com.xolo.core.service.WechatService; //service层代码 import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.util.List; @Api(tags = "微信号接口") //接口文档行头文字 @RestController @RequestMapping("/kpi/wechat") //接口请求地址 public class WechatController { @Autowired //用来给指定的字段或方法注入所需的外部资源 private WechatService wechatService; //每定义一个变量之前都需加一个@Autowired,否则引用变量语句要改为this.redisService @GetMapping("/list") //接口请求地址,请求方式为GET类型 @ApiOperation("获取用户下的所有微信号, 若user_id为0, 则获取全部微信号列表") //接口功能描述 public Response<List<Wechat>> getUserWechat(@RequestParam("pageNum") //请求参数标识,前端可见 @ApiParam("分页页码") //请求参数描述 Integer pageNum, @RequestParam("pageSize") @ApiParam("一页数据量") Integer pageSize, @RequestParam("user_id") @ApiParam(name = "user_id", value = "用户id", defaultValue = "1") Integer userId) { return wechatService.getUserWechat(pageNum,pageSize,userId); //接口具体实现代码放在Service层 } @PostMapping("/add") //请求方式为POST类型 @ApiOperation("添加微信号") public Response<Boolean> createWechat(@RequestBody WechatRequest wechatRequest) { return wechatService.createWechat(wechatRequest); } @PostMapping("/add/qrcode") @ApiOperation("新增或修改微信二维码") public Response<Boolean> addWechatQrcode(@RequestParam("wechat") @ApiParam("微信号") String wechat, @RequestParam("file") MultipartFile file) throws IOException { //接收前端form-data格式的文件 return wechatService.addWechatQrcode(wechat,file); } @PostMapping("/move/wechat") @ApiOperation("移动微信") public Response<Boolean> moveWechatUser(@RequestParam("wechat") @ApiParam("微信号List") List<String> wechatList, @RequestParam("user_id") @ApiParam("用户Id") Integer userId, HttpServletRequest request) {
//接收token字段,可根据token字段判断进行当前操作的用户信息 String token=request.getHeader("Authorization"); return wechatService.moveWechatUser(wechatList,userId,token); } @PostMapping("/modify/wechatName") @ApiOperation("修改微信名称") public Response<Boolean> updateWechatName(@RequestParam("wechat_id") @ApiParam("微信号Id") Integer wechatId, @RequestParam("wechat") @ApiParam("微信号") String wechat) { return wechatService.updateWechatName(wechatId,wechat); } }
扫码关注公众号,查看更多精彩内容
本文来自博客园,作者:不是公子的小白,转载请注明原文链接:https://www.cnblogs.com/bobbycheng/p/11850779.html