UserController.java 2.5 KB
Newer Older
Q
qinyingjie 已提交
1 2
package com.kwan.springbootkwan.controller;

Q
qinyingjie 已提交
3
import com.kwan.springbootkwan.entity.Student;
Q
qinyingjie 已提交
4 5
import com.kwan.springbootkwan.entity.User;
import com.kwan.springbootkwan.service.IUserService;
Q
qinyingjie 已提交
6 7
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
Q
qinyingjie 已提交
8
import lombok.extern.slf4j.Slf4j;
Q
qinyingjie 已提交
9
import org.springframework.beans.factory.annotation.Autowired;
Q
qinyingjie 已提交
10 11 12
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
Q
qinyingjie 已提交
13 14
import org.springframework.web.bind.annotation.*;

Q
qinyingjie 已提交
15 16
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
Q
qinyingjie 已提交
17 18
import java.util.List;

Q
qinyingjie 已提交
19 20 21 22 23 24 25
/**
 * 用户相关
 *
 * @author : qinyingjie
 * @version : 2.2.0
 * @date : 2022/12/19 16:08
 */
Q
qinyingjie 已提交
26
@Slf4j
Q
qinyingjie 已提交
27
@Api(description = "用户信息", tags = "UserController")
Q
qinyingjie 已提交
28 29 30 31 32 33 34
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

Q
qinyingjie 已提交
35
    @ApiOperation(value = "获取所有用户", notes = "获取所有用户")
Q
qinyingjie 已提交
36 37
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<User> addAdvertise() {
Q
qinyingjie 已提交
38
        log.info("测试日志={}", "success");
Q
qinyingjie 已提交
39 40 41
        return userService.getUsers();
    }

Q
qinyingjie 已提交
42
    @ApiOperation(value = "根据id获取用户信息", notes = "根据id获取用户信息")
Q
qinyingjie 已提交
43 44 45 46
    @RequestMapping(value = "/getUserById/{id}", method = RequestMethod.GET)
    public User getUserById(@PathVariable Integer id) {
        return userService.getUserById(id);
    }
Q
qinyingjie 已提交
47 48


49 50
    @PostMapping(value = "/student", produces = "application/json;charset=utf-8")
    public List<String> addUser(@RequestBody @Validated Student student, BindingResult result) throws UnsupportedEncodingException {
Q
qinyingjie 已提交
51 52 53 54 55 56 57 58 59 60
        List<String> errors = new ArrayList<>();
        if (result.hasErrors()) {
            List<ObjectError> allErrors = result.getAllErrors();
            for (ObjectError error : allErrors) {
                errors.add(error.getDefaultMessage());
            }
        }
        log.info("errors={}", errors);
        return errors;
    }
61 62 63 64 65 66

    @ApiOperation(value = "根据name获取用户信息", notes = "根据name获取用户信息")
    @RequestMapping(value = "/getUserByName", method = RequestMethod.GET)
    public User getUserByName(@RequestParam String sex) {
        return userService.getUserByName(sex);
    }
67 68 69 70 71 72 73 74 75


    @ApiOperation(value = "or拼接", notes = "or拼接")
    @RequestMapping(value = "/getUserByOr", method = RequestMethod.GET)
    public List<User> getUserByOr() {
        return userService.getUserByOr();
    }


Q
qinyingjie 已提交
76
}