首页>代码>搭建Spring Boot中使用Swagger2构建RESTful APIs>/sm/src/main/java/com/mch/boot/sm/controller/UserController.java
package com.mch.boot.sm.controller;

import com.mch.boot.sm.entity.User;
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 java.util.*;

/**
 * @Desc UserController
 * @Desc 测试 swgger 接口说明
 * @author mch
 */

@Api(tags = "接口文档说明")
@RestController
@RequestMapping(value="/users")     // 通过这里配置使下面的映射都在/users下,可去除
public class UserController {

    static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>());

    @ApiOperation(value="获取用户列表", notes="")
    @RequestMapping(value={"/list"}, method=RequestMethod.GET)
    public List<User> getUserList() {
        List<User> r = new ArrayList<User>(users.values());
        return r;
    }

    @ApiOperation(value="创建用户", notes="根据User对象创建用户")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    @RequestMapping(value="user", method=RequestMethod.POST)
    public String postUser(@RequestBody User user) {
        users.put(user.getId(), user);
        return "success";
    }

    @ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
    @RequestMapping(value="/{id}", method=RequestMethod.GET)
    public User getUser(@PathVariable Long id) {
        return users.get(id);
    }

    @ApiOperation(value="更新用户详细信息", notes="根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    })
    @RequestMapping(value="/{id}", method= RequestMethod.PUT)
    public String putUser(@PathVariable Long id, @RequestBody User user) {
        User u = users.get(id);
        u.setName(user.getName());
        u.setAge(user.getAge());
        users.put(id, u);
        return "success";
    }

    @ApiOperation(value="删除用户", notes="根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
    @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
    public String deleteUser(@PathVariable Long id) {
        users.remove(id);
        return "success";
    }

}
最近下载更多
大神程序员  LV23 7月10日
微信网友_6368711690080256  LV3 2023年2月28日
0592lyj  LV9 2023年2月16日
906933050  LV1 2022年10月9日
zhulei886  LV14 2021年5月19日
xhmpmail  LV17 2021年2月22日
lincolnpan  LV9 2021年2月2日
diadream  LV3 2021年1月18日
最代码官方  LV168 2021年1月10日
最近浏览更多
大神程序员  LV23 7月10日
内心向阳  LV4 2023年11月7日
微信网友_6368711690080256  LV3 2023年2月28日
jk-mack  LV5 2023年2月19日
0592lyj  LV9 2023年2月16日
906933050  LV1 2022年10月9日
wsupsup  LV16 2022年9月28日
哎呀马吖  LV6 2022年8月24日
2468867327  LV10 2022年5月17日
nbtest  LV1 2022年2月28日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友