美文网首页Spring Boot
springboot-Controller

springboot-Controller

作者: inke | 来源:发表于2017-07-07 18:36 被阅读142次

    Controller

    示例:

    HelloController

    package com.example;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
    
        @Value("${age}")
        private Integer age;//@value可以获取application.yml配置的值
    
        @RequestMapping(value = "/hello")
        public String sayHello() {
            return "hello world age:" + age;
        }
    }
    

    application.yml

    age: 18
    

    常用注解

    @RestController = @Controller + @ResponseBody
    @Controller需要返回 template 模版,类似于 Django 的模板。
    建议使用@RestController


    映射多个路径

    • 单个映射:@RequestMapping(value = "/hello")
    @RequestMapping(value = "/hello")
    public String sayHello() {
       return "hello world name:" + person.getName() + " , age:" + person.getAge();
    }
    
    • 多个映射:@RequestMapping(value = {"hello", "hi"})
    @RequestMapping(value = {"hello", "hi"})
    public String sayHello() {
       return "hello world name:" + person.getName() + " , age:" + person.getAge();
    }
    
    • 整个 controller 增加映射,在类上增加:@RequestMapping(value = "/demo")
    @RestController
    @RequestMapping(value = "/demo")
    public class HelloController {
    
        @Autowired
        private Person person;
    
        @RequestMapping(value = {"hello", "hi"})
        public String sayHello() {
            return "hello world name:" + person.getName() + " , age:" + person.getAge();
        }
    }
    

    访问方式:

    • method = RequestMethod.GET
    • method = RequestMethod.POST
    • ...
    • 简便方式:
      • @GetMapping(value = "hello")
      • @PostMapping(value = "hello")
    @RestController
    @RequestMapping(value = "/demo")
    public class HelloController {
    
        @Autowired
        private Person person;
    
        @RequestMapping(value = {"hello", "hi"}, method = RequestMethod.GET)
        //@GetMapping(value = "hello")
        public String sayHello() {
            return "hello world name:" + person.getName() + " , age:" + person.getAge();
        }
    }
    

    注意:什么都不写,默认是 get 和 post 都可以访问,但是不建议这样做,需要明确指定访问方式。


    获取访问参数

    • 获取 GETURL 的参数和 POST 的 请求 body 参数

      • 字段field映射
      @RequestMapping(value = "hello", method = RequestMethod.GET)
      public String sayHello(@RequestParam(value = "id", required = false, defaultValue = "0") Integer id) {
         return "id:" + id;
      }
      
      • 实体 model 映射
      @PostMapping(value = "/addPerson")
      public Person addPerson(Person person) {
         return personRespository.save(person);
      }
      
    • 获取 restfully 参数
    @RequestMapping(value = "hello/{id}", method = RequestMethod.GET)
    public String sayHello(@PathVariable("id") Integer id) {
       return "id:" + id;
    }
    

    相关文章

      网友评论

        本文标题:springboot-Controller

        本文链接:https://www.haomeiwen.com/subject/tvjrhxtx.html