Spring Boot学习笔记-常用注解总结

写在前面

  本来,是不想写这篇博客的,一是因为懒,二是因为之前学习过Spring MVC,而Spring Boot中的大部分常用注解其实都是Spring MVC中的注解。不过,为了以后方便自己查阅和养成写博客的习惯,我觉得我还是记录下来吧。


@Controller

  主要处理HTTP请求,Spring会将接收到的HTTP请求交给被@Controller所标记的类。现在强调前后台分离,所以,该注解现在主要与@ResponseBody配合使用来返回json数据。

    import org.springframework.stereotype.Controller;

    @Controller
    public class HelloController {

    }

@ResponseBody

作用:
  该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。
使用时机:
  返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;

  一般与@Controller配合使用来返回json数据。

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.ResponseBody;

    @Controller
    public class HelloController {

        @Autowired  //自动注入
        private Person person;

        @ResponseBody
        @RequestMapping(value = {"/hello", "/hi"}, method = RequestMethod.GET)
        public Person hello() {
            return person;
        }
    }

@RestController

  该注解是Spring4之后新加的注解,等同于@Controller@ResponseBody的组合。

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    public class HelloController {

        @Autowired
        private Person person;

        @RequestMapping(value = {"/hello", "/hi"}, method = RequestMethod.GET)
        public Person hello() {
            return person;
        }
    }

URL映射

  每个Controller类,接收到HTTP请求,会通过请求的URL和请求方法(GET,POST…)来映射使用哪个控制器方法来执行请求。这种映射主要通过@RequestMapping注解来实现。

单个URL映射

  一个控制器方法对应一个URL,注解@RequestMapping中,value的值即为所映射的URL。

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    public class HelloController {

        /**
         * 启动应用,浏览器打开http://localhost:8080/hello,会调用该方法,打印:Hello, Spring Boot.
         * @return
         */
        @RequestMapping(value = "/hello", method = RequestMethod.GET)
        public String helloGet() {
            return "Hello, Spring Boot";
        }

    }
不同方法的映射

  同一个URL,请求方法不同,也能对应不同的控制器方法。

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    public class HelloController {

        /**
         * 启动应用,浏览器打开http://localhost:8080/hello,会调用该方法,打印:Hello, Spring Boot.Request:GET.
         * @return
         */
        @RequestMapping(value = "/hello", method = RequestMethod.GET)
        public String helloGet() {
            return "Hello, Spring Boot.Request:GET";
        }

        /**
         * 启动应用后,通过模拟Http软件,以post方式请求http://localhost:8080/hello,会调用该方法,打印:Hello, Spring Boot.Request:POST.
         * HTTP模拟软件推荐postman。多平台支持。
         * @return
         */
        @RequestMapping(value = "/hello", method = RequestMethod.POST)
        public String helloPost() {
            return "Hello, Spring Boot.Request:POST";
        }
    }
映射的简写

  针对不同的请求方法,Spring都提供了它的简写方式,如@GetMapping@PostMapping。下面的代码与上面的代码实现的效果相同。

    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    public class HelloController {

        @GetMapping("/hello")  //等同于@RequestMapping(value = "/hello", method = RequestMethod.GET)
        public String helloGet() {
            return "Hello, Spring Boot.Request.GET";
        }

        @PostMapping("hello")  //等同于@RequestMapping(value = "/hello", method = RequestMethod.POST)
        public String helloPost() {
            return "Hello, Spring Boot.Request:POST";
        }
    }
多个URL映射

  一个控制器方法也可以对应多个URL,即value的值可以对应一个URL的集合。

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    public class HelloController {

        /**
         * 此时value对应两个URL,访问/hello和/hi是一样的效果
         * @return
         */
        @RequestMapping(value = {"/hello", "/hi"}, method = RequestMethod.GET)
        public String helloGet() {
            return "Hello, Spring Boot.";
        }

    }
类级URL映射

  @RequestMapping不只可以在方法上使用,也可以在一个控制器类上使用。在一个控制器类上使用该注解,那么类里的其它注解的URL,需要与该注解的URL相加进行访问才可以。代码如下:

    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    @RequestMapping("/howieli") //类级URL映射
    public class HelloController {

        /**
         * 此时访问该方法需要访问/howieli/hello或者/howieli/hi
         * @return
         */
        @RequestMapping(value = {"/hello", "/hi"}, method = RequestMethod.GET)
        public String helloGet() {
            return "Hello, Spring Boot.";
        }

    }

URL数据获取

  在实际开发中,参数肯定是不可或缺的一部分,那么,在控制器中应该怎么获取呢?我们需要知道,在实际开发中,传递的数据主要是分为两种的,第一种的直接通过URL传递,比如:http://localhost:8080/say/3,其中3为所传递数据。还有一种是通过参数传递,比如GET的传参方式:http://localhost:8080/say?id=3
  针对第一种情况,Spring提供了@PathVariable注解,针对第二种情况,Spring也提供了@RequestParam注解。

    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    public class HelloController {

        /**
         * 在URL映射的注解中,我们可以看到URL中被大括号括起来的id,这就代表我们要传递的数据
         * 在helloGet方法传入参数时使用@PathVariable注解,表示将URL中的id所代表的数据作为参数传入方法中
         * name这个参数,是通过参数的方式传入,此时可以使用@RequestParam注解,其实在这里注解可以省略不写,因为我们变量名是一样的
         * 比如我们参数是?name=howieli中的name与方法参数name是相同的,就可以省略注解。
         * @param id
         * @return
         */
        @GetMapping(value = "/say/{id}")
        public String helloGet(@PathVariable("id") int id, @RequestParam("name") String name) {
            return "id: " +  id + ",name:" + name;
        }

    }

  启动应用,访问http://localhost:8080/say/5?name=howieli,即可打印id: 5,name:howieli
  这个部分写的有点乱。


结语

  这只是一些比较常用的注解,之后碰到其它重要注解会慢慢补充。
  个人博客:https://www.howieli.cn 和个人CSDN博客: http://blog.csdn.net/howieli_1995

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值