spring常用注解

  • SpringMVC常用注解: 
    • @Component:通用  
    • @Controller: controller层使用
    • @Service: service层使用
    • @Repository: dao层使用

    • @PathVariable: 映射URL绑定的占位符
      • 例:访问 xxx/testPathVariable/1
        //@PathVariable可以用来映射URL中的占位符到目标方法的参数中
        @RequestMapping("/testPathVariable/{id}")
        public String testPathVariable(@PathVariable("id") Integer id){
            System.out.println("testPathVariable:"+id);
            return SUCCESS;
        }
      @ModelAttribute:
      • 注解标记在方法上
      • 每次访问该Controller的方法都会先执行被@ModelAttribute标记的方法(多用于init())
      @RequestParam:映射URL上的参数
      • 例:访问 xxx/user?id=1  
        @RequestMapping("/user")
        @ResponseBody
        public String getUserBlog(@RequestParam("id") int blogId) {
            return "blogId = " + blogId;
        }
      • 注:可以加上 require=false,default='xxx',则不传该参数不会报错
        @RequestMapping("/user")
        @ResponseBody
        public String getUserBlog(@RequestParam(value="id",require=false,default='0') int blogId) {
            return "blogId = " + blogId;
        }
  • SpringBoot常用注解
    • @RestController:相当于@Controller+@ResponseBody
原文地址:https://www.cnblogs.com/linhuanjie/p/9608951.html