SpringBoot常用注解

个人Controller层使用过的注解

@CrossOrigin

主要用途解决跨域问题

@RestController

主要作用是@Controller + @ResponseBody
然后了解一下@Controller + @ResponseBody
@Controller主要是说明这一层是控制层
@ResponseBody表示方法的返回值直接以指定的格式写入Http response body中,而不是解析为跳转路径

@RequestMapping

用来处理地址映射请求的注解

@RequestMapping有六个参数,列出主要三个

  1. value
    用于设置方法或者类的映射路径,可以直接写路径
    主要代码:
    @RequestMapping("/Circle")
  2. method
    用于指定请求的方法,可以设置单个或多个,如果请求方法不满足条件则会请求失败。
  3. headers
    用于指定请求的headers,必须要含有这个headers才可以请求

@Autowired

它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作
注释使用方法:

    @Autowired
    ExcelTool excelTool;

@GetMapping

@GetMapping注解已经默认封装了@RequestMapping(method = RequestMethod.GET)

即@GetMapping只能使用GET请求

@GetMapping("GetCircleById")
    private String GetCircleById(@RequestParam("id") int id) {
        resultDao = new ResultDao();
        try {
            AddcircleEntity addcircleEntity = addCircleService.GetCircleById(id);
            if (addcircleEntity != null) {
                resultDao.setCode("0");
                resultDao.setMsg("获取成功!");
                resultDao.setData(addcircleEntity);
            } else {
                resultDao.setCode("1");
                resultDao.setMsg("获取失败!");
            }
        } catch (Exception e) {
            resultDao.setCode("1");
            resultDao.setMsg("获取失败!");
        }
        return JSONTool.getJsonTool().toJson(resultDao);
    }

同理还有@PostMapping、@DeleteMapping

都是将@RequestMapping的请求方法封装起来了的

@RequestParam

@RequestParam是用来接收前端传过来的值的
例如@RequestParam("limit") int limit
如果你写了并且前端不传或者传错是都会报错的,因为不传或者传错后端是接收不到传的值,然后会给参数赋值mull,然后mull传给了int,因此报错

@CheckToken

作用是接口验证

调用下面的方法

@Component
public class TokenTool {
    //根据Email和密码 生成唯一的Token
    public String getToken(UserEntity userEntity){
        return JWT.create().withAudience(userEntity.getEmail()).sign(Algorithm.HMAC256(userEntity.getPwd()));
    }
}