UserController.java 2.4 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;
9
import org.apache.ibatis.annotations.Param;
Q
qinyingjie 已提交
10
import org.springframework.beans.factory.annotation.Autowired;
Q
qinyingjie 已提交
11 12 13 14
import org.springframework.http.HttpRequest;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
Q
qinyingjie 已提交
15 16
import org.springframework.web.bind.annotation.*;

Q
qinyingjie 已提交
17 18 19
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
Q
qinyingjie 已提交
20 21
import java.util.List;

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

    @Autowired
    private IUserService userService;

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

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


52 53
    @PostMapping(value = "/student", produces = "application/json;charset=utf-8")
    public List<String> addUser(@RequestBody @Validated Student student, BindingResult result) throws UnsupportedEncodingException {
Q
qinyingjie 已提交
54 55 56 57 58 59 60 61 62 63
        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;
    }
64 65 66 67 68 69

    @ApiOperation(value = "根据name获取用户信息", notes = "根据name获取用户信息")
    @RequestMapping(value = "/getUserByName", method = RequestMethod.GET)
    public User getUserByName(@RequestParam String sex) {
        return userService.getUserByName(sex);
    }
Q
qinyingjie 已提交
70
}