1. 程式人生 > >Spring Boot之 Controller 接收引數和返回資料總結(包括上傳、下載檔案)

Spring Boot之 Controller 接收引數和返回資料總結(包括上傳、下載檔案)

        一、接收引數(postman傳送) 1.form表單 @RequestParam("name") String name 會把傳遞過來的Form表單中的name對應到formData方法的name引數上 該註解不能接收json傳參 該註解表示name欄位是必須入參的,否則會報錯 @RequestParam(value = "name", required = false) String name required = false表示必須入參 @RequestParam(value = "name", defaultValue = "admin") String name defaultValue = "admin"表示當name入參為空的時候給它一個預設值admin
    /**
* 測試接收form表單、URL的資料。不能接收Json資料 * */ @RequestMapping(value = "/test1", method = RequestMethod.POST) public String formData(@RequestParam("name") String name , @RequestParam("age") int age){ String result = "receive name = "+name+" age = "+age; System.out.println(result);
return result; }

2.URL

程式碼跟1.form表單中的程式碼一樣

3.動態接收URL中的資料 @PathVariable將URL中的佔位符引數繫結到控制器處理方法的入參 此種情況下,url求情中一定要帶佔位符pageNo,pageSize的值,不然訪問失敗 即訪問時一定要用 http://localhost:8088/sid/test2/2/20 如果用 http://localhost:8088/sid/test2 則訪問失敗
    /**

     * 測試動態接收URL中的資料

     * 
*/ @RequestMapping(value = "/test2/{pageNo}/{pageSize}", method = RequestMethod.POST) public String urlData(@PathVariable int pageNo , @PathVariable int pageSize){ String result = "receive pageNo = "+pageNo+" pageSize = "+pageSize; System.out.println(result); return result; }

 

4.json

@RequestBody 接收Json格式的資料需要加這個註解。該註解不能接收URL、Form表單傳參

    /**

     * 測試接收json資料

     * */

    @RequestMapping(value = "/jsonData", method = RequestMethod.POST)

    public String jsonData(@RequestBody TestModel tm){

        String result = "receive name = "+tm.getName()+" age = "+tm.getAge();

        System.out.println(result);

        return result;

    }

5.@RequestMapping註解詳細介紹

1.處理多個URL

@RestController  

@RequestMapping("/home")  

public class IndexController {  

  

    @RequestMapping(value = {  

        "",  

        "/page",  

        "page*",  

        "view/*,**/msg"  

    })  

    String indexMultipleMapping() {  

        return "Hello from index multiple mapping.";  

    }  

} 
這些 URL 都會由 indexMultipleMapping() 來處理:  localhost:8080/home localhost:8080/home/ localhost:8080/home/page localhost:8080/home/pageabc localhost:8080/home/view/ localhost:8080/home/view/view   2.HTTP的各種方法 如POST方法 @RequestMapping(value = "/test1", method = RequestMethod.POST) 3.produces、consumes  produces 指定返回的內容型別,僅當request請求頭header中的(Accept)型別中包含該指定型別才返回。結合@ResponseBody使用
---------------------
@Controller
@RequestMapping(value = "/t")
public class TestController {
 
    //方法僅處理request請求中Accept頭中包含了"text/html"的請求
    @ResponseBody
    @RequestMapping(value = "/produces",produces = {"text/html"})
    public String testProduces(String name)
    {
        return "test requestMapping produces attribute! "+name;
    }
}

方法僅處理request請求中Accept頭中包含了"text/html"的請求

比如用postman構建一個Accept=“application/json”的請求,請求會失敗

 

comsumes  指定處理請求的提交內容型別(Content-Type),例如application/json, text/html。結合@RequestBody使用

@Controller
@RequestMapping(value = "/t")
public class TestController {
 
    //方法僅處理request Content-Type為"application/json"型別的請求
    @ResponseBody
    @RequestMapping(value = "/consumes",consumes = {"application/json"})
    public String testConsumes(@RequestBody String name)
    {
        return "test requestMapping consumes attribute! "+name;
    }
}

方法僅處理request Content-Type為"application/json"型別的請求。

如果用postman構建一個Content-Type=“application/x-www-form-urlencoded”的請求,該方法不處理

 

4.headers 

根據請求中的訊息頭內容縮小請求對映的範圍

例如:

只處理header中testHeader = sid的請求

    //方法僅處理header中testHeader = sid的請求
    @ResponseBody
    @RequestMapping(value = "/header",headers = {"testHeader = sid"})
    public String testHeader(String name)
    {
        return "test requestMapping headers attribute! "+name;
    }

構建一個header鐘不帶testHeader=sid的請求,會失敗

必須要header中帶testHeader=sid的請求的請求才處理

 

 

 

5.結合params屬性處理請求引數

例如:

請求引數name=sid的時候由getParams方法處理

請求引數name=lee的時候由getParamsDifferent方法處理

@Controller
@RequestMapping(value = "/t")
public class TestController {
 
    @RequestMapping(value = "/params", params = {
            "name=sid"
    })
    @ResponseBody
    public String getParams(@RequestParam("name") String name) {
        return "getParams method do " + name;
    }
    @RequestMapping(value = "/params", params = {
            "name=lee"
    })
    @ResponseBody
    public String getParamsDifferent(@RequestParam("name") String name) {
        return "getParamsDifferent method do  " + name;
    }
}