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>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章