UserController.java 2.1 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 13
import org.springframework.http.HttpRequest;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
Q
qinyingjie 已提交
14 15
import org.springframework.web.bind.annotation.*;

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

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

    @Autowired
    private IUserService userService;

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

Q
qinyingjie 已提交
44
    @ApiOperation(value = "根据id获取用户信息", notes = "根据id获取用户信息")
Q
qinyingjie 已提交
45 46 47 48
    @RequestMapping(value = "/getUserById/{id}", method = RequestMethod.GET)
    public User getUserById(@PathVariable Integer id) {
        return userService.getUserById(id);
    }
Q
qinyingjie 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62


    @PostMapping(value = "/student",produces = "application/json;charset=utf-8")
    public List<String> addUser(@RequestBody @Validated Student student, BindingResult result ) throws UnsupportedEncodingException {
        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;
    }
Q
qinyingjie 已提交
63
}