Spring Boot簡明教程之實現Web開發及常用參數獲取方式分析

Spring Boot簡明教程之實現Web開發及常用參數獲取方式分析

在前面的文章中,我們已經大致介紹了有關Spring Boot的項目配置等信息,我們也體會到了Spring Boot對於Web開發的支持。在這篇文章中,我們將介紹Spring Boot對於Web開發的支持:

Controller的使用

在MVC類型的項目開發中,控制器(Controller)是十分重要的一個環節,其中,最重要的就是下面四個註解:

註解 解釋
@Controller 處理請求
@ResponseBody 將返回的數據轉換爲 JSON 格式
@RestController @RestController=@Controller+@ResponseBody
@RequestMapping 配置URL映射

請求處理:@RestController

控制器(Controller)是MVC架構中最重要的一環,同時JSON對於前後端分離的項目中,前後端數據交流的重要傳輸方式,對於這二者的支持,對於Web開發來說是十分重要的。我們都知道@Controller用於處理http請求,而@ResponseBody是將數據轉換爲JSON數據,而@RestController就更像是兩者的結合。

例如:

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }
}

我們在瀏覽器輸入:http://localhost:8080/hello

在這裏插入圖片描述
成功訪問,但是如果我們將@RestController更改爲@Controller,就會發現:訪問報錯,大概意思就是我們缺少視圖的訪問模板,這是因爲@Controller更多時候是搭配着視圖模板進行使用(這點我們會在後面進行詳細的介紹)

@Controller
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }
}

在這裏插入圖片描述

當我們加上@ResponseBody後,訪問又再次成功。

@Controller
@ResponseBody
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }
}

這就是證明了:@RestController=@Controller+@ResponseBody。

當我們進入@RestController源碼查看時,發現:再次印證了這一點

在這裏插入圖片描述


路由設置:@RequestMapping

【@RequestMapping】 用於配置URL映射,即可配置與類,也可以配置與方法。配置於類表示該類訪問的父路徑,配置於方法則表示該方法的實際映射地址。

該註解有三個常用的屬性:value、method 和 produces。

屬性 說明
value 指定請求的實際地址,可以省略不寫
method 指定請求的類型,默認爲 GET
produces 指定返回內容類型,如 produces = "application/json; charset=UTF-8"

例如,我們將之前的HelloController更改爲:

@RestController
@RequestMapping(value = "/test")
public class HelloController {

    @RequestMapping(value = "/hello")
    public String hello(){
        return "hello";
    }
}

我們就發現,訪問的地址已經變成了:http://localhost:8080/test/hello

在這裏插入圖片描述

如果我們想讓多個URL能訪問同一個方法,應該怎麼做呢?

@RestController
@RequestMapping(value = "/test")
public class HelloController {
    @RequestMapping(value = {"/hello","hello2"})
    public String hello(){
        return "hello";
    }
}

我們就發現:地址變成了一個數組,只要是這個數組內的url均能訪問該方法。

在這裏插入圖片描述

如果我們想指定訪問的方法:

@RestController
@RequestMapping(value = "/test")
public class HelloController {

    @RequestMapping(value = "/hello" ,method = RequestMethod.POST)
    public String hello(){
        return "hello";
    }
}

再次啓動,我們就發現:訪問方法被限制爲【POST】

在這裏插入圖片描述

如果我們再在瀏覽器輸入:http://localhost:8080/test/hello訪問,就會被拒絕:

在這裏插入圖片描述

除此之外呢,還有更加簡便的方式來實現請求方式的限制:

註釋 解釋
@DeleteMapping DELETE方法
@GetMapping GET方法
@PutMapping PUT方法
@PostMapping POST方法

例如:

@RestController
@RequestMapping(value = "/test")
public class HelloController {

//    @RequestMapping(value = "/hello" ,method = RequestMethod.GET,produces = "application/json; charset=UTF-8")
    @GetMapping("/hello")
    public String hello(){
        return "hello";
    }
}

啓動:效果和 @RequestMapping一樣,而且更加簡潔。

在這裏插入圖片描述


參數獲取

對於參數獲取,主要使用的是下面三個註解:

註解 說明
@PathVariable 獲取 URL 參數
@RequestParam 從 Request 裏獲取參數值
@RequestBody 獲取JSON實體

@PathVariable

使用@PathVariable獲取參數,更多的是針對http://localhost:8080/test/hello/{phone}這類動態參數,例如:

@RestController
@RequestMapping(value = "/test")

public class HelloController {

    @RequestMapping(value = "/hello/{phone}")
    public Integer phone(@PathVariable Integer phone ){
        return phone;
    }
}

啓動項目,並在瀏覽器輸入:http://localhost:8080/test/hello/546

在這裏插入圖片描述

如果有多個動態參數:

@RestController
@RequestMapping(value = "/test")

public class HelloController {

    @RequestMapping(value = "/hello/{phone}/{name}")
    public String phone(@PathVariable Integer phone,@PathVariable String name){
        return "電話:"+ phone +"\n"+ "姓名: " + name;
    }
}

啓動項目,在瀏覽器輸入:http://localhost:8080/test/hello/546/name

在這裏插入圖片描述

這樣就獲取了多個參數,但是需要注意的是: URL 中的參數和方法中的參數名需要保持一致。如果不同,那麼就需要指定URL中的參數名來獲取,例如:

@RestController
@RequestMapping(value = "/test")
public class HelloController {

    @RequestMapping(value = "/hello/{phone}/{name}")
    public String phone(@PathVariable(value = "phone") Integer myPhone,@PathVariable String name){
        return "電話:"+ myPhone +"\n"+ "姓名: " + name;
    }
}

這裏就不再展示運行結果。

@RequestParam

@RequestParam獲取參數主要針對URL爲:http://localhost:8080/test/hello/?phone=456

例如:

@RestController
@RequestMapping(value = "/test")

public class HelloController {

    @RequestMapping(value = "/hello")
    public Integer phone(@RequestParam Integer phone){
        return phone;
    }
}

啓動項目,輸入:http://localhost:8080/test/hello/?phone=456

在這裏插入圖片描述

但需要注意的是,如果URL 中的參數和方法中的參數名不一致,仍然需要指定參數名,例如:

@RestController
@RequestMapping(value = "/test")

public class HelloController {
    @RequestMapping(value = "/hello")
    public Integer phone(@RequestParam(value = "phone") Integer myPhone){
        return myPhone;
    }
}

這裏就不再演示運行結果。同時@RequestParam還有兩個常用的屬性:

屬性 說明
required 參數是否爲必須,True 表示爲必須;
defaultValue 默認值,當獲取到的參數爲Null時使用

例如:

@RestController
@RequestMapping(value = "/test")

public class HelloController {
    @RequestMapping(value = "/hello")
    public Integer phone(@RequestParam(value = "phone",required = false,defaultValue = "139") Integer myPhone){
        return myPhone;
    }
}

啓動項目,瀏覽器輸入:http://localhost:8080/test/hello/

在這裏插入圖片描述

我們發現,當我們爲傳值時,返回的我們設置的默認值:139。

@RequestBody

@RequestBody主要用來獲取前端傳來的封裝好的Json格式的參數。

例如:

@RestController
@RequestMapping(value = "/test")

public class HelloController {

    @RequestMapping(value = "/hello",method = RequestMethod.POST)
    public String hello( @RequestBody String str){
        return str;
    }
}

然後,我們使用Postman進行Post請求測試(因爲瀏覽器不方便對Post請求進行測試,Postman是後端開發中比較常用的接口測試工具,官網地址

在這裏插入圖片描述


Json的使用

我們在上文提到過,@RestController可以將返回參數轉爲Json格式。那麼我們現在就來感受下Spring Boot對於Json的支持。

實體類

創建如下的目錄結構

在這裏插入圖片描述

其中,Person類爲:

package cn.newtol.springboot05.entity;

/**
 * @Author: REN
 * @Description:
 * @Date: Created in 20:10 2018/9/17
 */

public class Person {
    private String name;
    private Integer phone;

    public Person(String name, Integer phone) {
        this.name = name;
        this.phone = phone;
    }
    ...
    /* 省略get、set方法 */
	
}

HelloController爲:

@RestController
@RequestMapping(value = "/test")

public class HelloController {

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

啓動項目,瀏覽器輸入:http://localhost:8080/test/person

在這裏插入圖片描述

List

將HelloController改爲:

@RestController
@RequestMapping(value = "/test")

public class HelloController {

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<String> list(){
        List<String> lists = new ArrayList<>();
        lists.add("abc");
        lists.add("cdf");
        return lists;
    }
}

啓動項目,瀏覽器輸入:http://localhost:8080/test/list

在這裏插入圖片描述
我們就成功的將List轉換爲了Json格式,對於其他的數據類型,我們就不再進行演示,有興趣的小夥伴可以自行進行測試。

總結

我們主要介紹了Spring Boot對於Web開發的一些支持,其中主要介紹了:@RestController 、 @RequestMapping、@PathVariable 、@RequestParam 、@RequestBody五個註解,以及演示了一下Spring Boot對於Json的支持。

源碼地址

源碼地址

關注博主的個人公衆號,有更多視頻學習資源分享~
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章