package com.cjt.web;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.List;import java.util.Map;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.PathVariable;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.RestController;import com.cjt.config.JsonResult;import comcjt.entity.User;import io.swagger.annotations.ApiImplicitParam;import io.swagger.annotations.ApiImplicitParams;import io.swagger.annotations.ApiOperation;import springfox.documentation.annotations.ApiIgnore;@RestControllerpublic class UserController {// 创建线程安全的Mapstatic Map<Integer, User> users = Collections.synchronizedMap(new HashMap<Integer, User>());/** * 根据ID查询用户 * @param id * @return */@ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path")@RequestMapping(value = "user/{id}", method = RequestMethod.GET)public ResponseEntity<JsonResult> getUserById (@PathVariable(value = "id") Integer id){JsonResult r = new JsonResult();try {User user = users.get(id);r.setResult(user);r.setStatus("ok");} catch (Exception e) {r.setResult(e.getClass().getName() ":" e.getMessage());r.setStatus("error");e.printStackTrace();}return ResponseEntity.ok(r);}/** * 查询用户列表 * @return */@ApiOperation(value="获取用户列表", notes="获取用户列表")@RequestMapping(value = "users", method = RequestMethod.GET)public ResponseEntity<JsonResult> getUserList (){JsonResult r = new JsonResult();try {List<User> userList = new ArrayList<User>(users.values());r.setResult(userList);r.setStatus("ok");} catch (Exception e) {r.setResult(e.getClass().getName() ":" e.getMessage());r.setStatus("error");e.printStackTrace();}return ResponseEntity.ok(r);}/** * 添加用户 * @param user * @return */@ApiOperation(value="创建用户", notes="根据User对象创建用户")@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")@RequestMapping(value = "user", method = RequestMethod.POST)public ResponseEntity<JsonResult> add (@RequestBody User user){JsonResult r = new JsonResult();try {users.put(user.getId(), user);r.setResult(user.getId());r.setStatus("ok");} catch (Exception e) {r.setResult(e.getClass().getName() ":" e.getMessage());r.setStatus("error");e.printStackTrace();}return ResponseEntity.ok(r);}/** * 根据id删除用户 * @param id * @return */@ApiOperation(value="删除用户", notes="根据url的id来指定删除用户")@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")@RequestMapping(value = "user/{id}", method = RequestMethod.DELETE)public ResponseEntity<JsonResult> delete (@PathVariable(value = "id") Integer id){JsonResult r = new JsonResult();try {users.remove(id);r.setResult(id);r.setStatus("ok");} catch (Exception e) {r.setResult(e.getClass().getName() ":" e.getMessage());r.setStatus("error");e.printStackTrace();}return ResponseEntity.ok(r);}/** * 根据id修改用户信息 * @param user * @return */@ApiOperation(value="更新信息", notes="根据url的id来指定更新用户信息")@ApiImplicitParams({@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long",paramType = "path"),@ApiImplicitParam(name = "user", value = "用户实体user", required = true, dataType = "User")})@RequestMapping(value = "user/{id}", method = RequestMethod.PUT)public ResponseEntity<JsonResult> update (@PathVariable("id") Integer id, @RequestBody User user){JsonResult r = new JsonResult();try {User u = users.get(id);u.setUsername(user.getUsername());u.setAge(user.getAge());users.put(id, u);r.setResult(u);r.setStatus("ok");} catch (Exception e) {r.setResult(e.getClass().getName() ":" e.getMessage());r.setStatus("error");e.printStackTrace();}return ResponseEntity.ok(r);}@ApiIgnore//使用该注解忽略这个API@RequestMapping(value = "/hi", method = RequestMethod.GET)public String jsonTest() {return " hi you!";}/*@Api:修饰整个类,描述Controller的作用 @ApiOperation:描述一个类的一个方法,或者说一个接口 @ApiParam:单个参数描述 @ApiModel:用对象来接收参数 @ApiProperty:用对象接收参数时,描述对象的一个字段 @ApiResponse:HTTP响应其中1个描述 @ApiResponses:HTTP响应整体描述 @ApiIgnore:使用该注解忽略这个API @ApiError :发生错误返回的信息 @ApiImplicitParam:一个请求参数 @ApiImplicitParams:多个请求参数 */}
下载swagger+springboot 入门级示例用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论