zoukankan      html  css  js  c++  java
  • springboot中controller的使用

    一、知识点

    1 @Controller 处理http请求(不推荐使用)
    2 @RestController spring4之后新加的注解,原来返回json需要@ResponseBody配合@Controller
    3 @RequestMapping 配置Url映射
    4 @GetMapping 当 在方法上的注释时@RequestMapping的简化版,推荐使用,类似的还有@PostMapping等等
    5 @RequestParam 映射请求参数到java方法的参数
    6 @PageableDefault 指定分页参数默认值
    7 @RequestBody 注解用于将Controller的方法参数,根据HTTP Request Header的content-Type的内容,通过适当的HttpMessageConverter转换为JAVA类

    二、具体使用讲解

    1.@Controller(了解即可,现在的开发基本都是前后端分离,不用再使用模版的方式,采用REST方式返回json数据是主流)

    需要配合模版的使用

    1)打开pom.xml

    添加spring官方的一个模版thymeleaf

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>

    2)在resources下新建文件夹templates,然后在其中新建一个html,index.html

    <h1>hello spring boot!</h1>

    3)controller中将@RestController改为@Controller

    package com.dechy.girl.girl;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    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.RestController;
    
    @Controller
    public class HelloController {
    
        @Autowired
        private GirlProperties girlProperties;
    
        @RequestMapping(value = "/hello", method = RequestMethod.GET)
        public String say (){
            return "index";
        }
    }

    4)启动后,访问得到index.html的内容

    2.@RestController

    他是@Controller和@ResponseBody的组合

    1)修改controller文件

    package com.dechy.girl.girl;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    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;
    import org.springframework.web.bind.annotation.RestController;
    
    @Controller
    @ResponseBody
    public class HelloController {
    
        @Autowired
        private GirlProperties girlProperties;
    
        @RequestMapping(value = "/hello", method = RequestMethod.GET)
        public String say (){
            return girlProperties.getCupSize();
        }
    }

    此处为了简便,采用@RestController即可

    3.@RequestMapping

    1)可以设置映射多个地址,这样访问多个地址能可以返回相同的内容,例如

    package com.dechy.girl.girl;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    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 GirlProperties girlProperties;
    
        @RequestMapping(value = {"/hello","/hi"}, method = RequestMethod.GET)
        public String say (){
            return girlProperties.getCupSize();
        }
    }

    2)可以给整个类映射一个url,如下, 访问http://127.0.0.1:8082/hello/say即可

        package com.dechy.girl.girl;
    
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.beans.factory.annotation.Value;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RequestMethod;
        import org.springframework.web.bind.annotation.RestController;
    
        @RestController
        @RequestMapping("/hello")
        public class HelloController {
    
            @Autowired
            private GirlProperties girlProperties;
    
            @RequestMapping(value = "/say", method = RequestMethod.GET)
            public String say (){
                return girlProperties.getCupSize();
            }
        }

    3)处理url的参数

    @PathVariable
    获取url中的数据
    @RequestParam
    获取请求参数的值
    @GetMapping
    组合注解
    @PageableDefault
     指定分页参数默认值

    i)@PathVariable

        package com.dechy.girl.girl;
    
        import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
        @RestController
        @RequestMapping("/hello")
        public class HelloController {
    
            @Autowired
            private GirlProperties girlProperties;
    
            @RequestMapping(value = "/say/{id}", method = RequestMethod.GET)
            public String say (@PathVariable("id") Integer id){
                return "id:"+id;
            }
        }

    或者改成

     @RequestMapping(value = "/{id}/say", method = RequestMethod.GET)

    ii)@RequestParam

    对于传统的url.如http://127.0.0.1:8082/hello/say?id=111

        package com.dechy.girl.girl;
    
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.web.bind.annotation.*;
    
        @RestController
        @RequestMapping("/hello")
        public class HelloController {
    
            @Autowired
            private GirlProperties girlProperties;
    
            @RequestMapping(value = "/say", method = RequestMethod.GET)
            public String say (@RequestParam("id") Integer myId){
                return "id:"+myId;
            }
        }

    其中黄色的不用一直,id对应的是url中的参数,myId可以自定义

    另外,可以给@RequestParam设置默认值

        package com.dechy.girl.girl;
    
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.web.bind.annotation.*;
    
        @RestController
        @RequestMapping("/hello")
        public class HelloController {
    
            @Autowired
            private GirlProperties girlProperties;
    
            @RequestMapping(value = "/say", method = RequestMethod.GET)
            public String say (@RequestParam(value="id",required = false,defaultValue = "0") Integer myId){
                return "id:"+myId;
            }
        }

    iii)@GetMapping(推荐)

    用@RequestParam的话,感觉代码太长,那么就可以使用@GetMapping,另外还可以有@PostMapping @PutMapping等等

        package com.dechy.girl.girl;
    
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.web.bind.annotation.*;
    
        @RestController
        @RequestMapping("/hello")
        public class HelloController {
    
            @Autowired
            private GirlProperties girlProperties;
    
           @GetMapping(value="/say")
            public String say (@RequestParam(value="id",required = false,defaultValue = "0") Integer myId){
                return "id:"+myId;
            }
        }

     iiii)@PageableDefault

    指定指定分页参数默认值。

     @RequestMapping(value = "/user",method = RequestMethod.GET)
        public List<User> query(UserQueryCondition userQueryCondition, @PageableDefault(page=1,size=10,sort="username,asc") Pageable pageable){
            System.out.println(pageable.getPageNumber());
            System.out.println(pageable.getSort());
            System.out.println(pageable.getPageSize());
            System.out.println(userQueryCondition.toString());
            List<User> users=new ArrayList<>();
            users.add(new User());
            users.add(new User());
            users.add(new User());
            return  users;
        }

    备注:此处Pageable需要引入如下依赖

    <dependency>
         <groupId>org.springframework.data</groupId>
         <artifactId>spring-data-jpa</artifactId>
    </dependency>
  • 相关阅读:
    付出给人一种美好的感觉
    表连接查询 条件在On与Where后区别
    Json与实体类 转化
    算法的时间复杂度和空间复杂度详解
    真的理解同步和异步了吗?
    花生壳申请域名并进行内网穿透
    XML 反序列化
    .Net 发送邮件
    BootStrap DataTable 时间日期列排序
    SQL Server 动态SQL拼接
  • 原文地址:https://www.cnblogs.com/knyel/p/7800303.html
Copyright © 2011-2022 走看看