SpringBoot - 獲取POST請求參數詳解(附樣例:表單數據、json、數組、對象)


    在前文中我介紹了 Controller 如何接收通過 GET 方式傳遞過來的參數( 點擊查看),下面接着演示如何接收通過 POST 方式傳遞過來的參數。

一、接收 Form 表單數據

1,基本的接收方法

(1)下面樣例 Controller 接收 form-data 格式的 POST 數據:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package  com.example.demo;
 
import  org.springframework.web.bind.annotation.PostMapping;
import  org.springframework.web.bind.annotation.RequestParam;
import  org.springframework.web.bind.annotation.RestController;
 
@RestController
public  class  HelloController {
     @PostMapping ( "/hello" )
     public  String hello( @RequestParam ( "name" ) String name,
                         @RequestParam ( "age" ) Integer age) {
         return  "name:"  + name +  "\nage:"  + age;
     }
}
 
(2)下面是一個簡單的測試樣例:
原文:SpringBoot - 獲取POST請求參數詳解(附樣例:表單數據、json、數組、對象)
 

2,參數沒有傳遞的情況

(1)如果沒有傳遞參數 Controller 將會報錯,這個同樣有如下兩種解決辦法:
  • 使用 required = false 標注參數是非必須的。
  • 使用 defaultValue 給參數指定個默認值。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package  com.example.demo;
 
import  org.springframework.web.bind.annotation.PostMapping;
import  org.springframework.web.bind.annotation.RequestParam;
import  org.springframework.web.bind.annotation.RestController;
 
@RestController
public  class  HelloController {
     @PostMapping ( "/hello" )
     public  String hello( @RequestParam (name =  "name" , defaultValue =  "xxx" ) String name,
                         @RequestParam (name =  "age" , required =  false ) Integer age) {
         return  "name:"  + name +  "\nage:"  + age;
     }
}
 
(2)下面是一個簡單的測試樣例:
原文:SpringBoot - 獲取POST請求參數詳解(附樣例:表單數據、json、數組、對象)
 

3,使用 map 來接收參數

(1)Controller 還可以直接使用 map 來接收所有的請求參數:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package  com.example.demo;
 
import  org.springframework.web.bind.annotation.PostMapping;
import  org.springframework.web.bind.annotation.RequestParam;
import  org.springframework.web.bind.annotation.RestController;
 
import  java.util.Map;
 
@RestController
public  class  HelloController {
     @PostMapping ( "/hello" )
     public  String hello( @RequestParam  Map<String,Object> params) {
         return  "name:"  + params.get( "name" ) +  "\nage:"  + params.get( "age" );
     }
}

(2)下面是一個簡單的測試樣例:
原文:SpringBoot - 獲取POST請求參數詳解(附樣例:表單數據、json、數組、對象)

4,接收一個數組

(1)表單中有多個同名參數,Controller 這邊可以定義一個數據進行接收:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package  com.example.demo;
 
import  org.springframework.web.bind.annotation.PostMapping;
import  org.springframework.web.bind.annotation.RequestParam;
import  org.springframework.web.bind.annotation.RestController;
 
import  java.util.Map;
 
@RestController
public  class  HelloController {
     @PostMapping ( "/hello" )
     public  String hello( @RequestParam ( "name" ) String[] names) {
         String result =  "" ;
         for (String name:names){
             result += name +  "\n" ;
         }
         return  result;
     }
}

(2)下面是一個簡單的測試樣例:
原文:SpringBoot - 獲取POST請求參數詳解(附樣例:表單數據、json、數組、對象)

5,使用對象來接收參數

(1)如果一個 post 請求的參數太多,我們構造一個對象來簡化參數的接收方式:
1
2
3
4
5
6
7
8
9
10
11
12
package  com.example.demo;
 
import  org.springframework.web.bind.annotation.PostMapping;
import  org.springframework.web.bind.annotation.RestController;
 
@RestController
public  class  HelloController {
     @PostMapping ( "/hello" )
     public  String hello(User user) {
         return  "name:"  + user.getName() +  "\nage:"  + user.getAge();
     }
}

(2)User 類的定義如下,到時可以直接將多個參數通過 getter、setter 方法注入到對象中去:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package  com.example.demo;
 
public  class  User {
     private  String name;
     private  Integer age;
 
     public  String getName() {
         return  name;
     }
 
     public  void  setName(String name) {
         this .name = name;
     }
 
     public  Integer getAge() {
         return  age;
     }
 
     public  void  setAge(Integer age) {
         this .age = age;
     }
}
 
(3)下面是一個簡單的測試樣例:
原文:SpringBoot - 獲取POST請求參數詳解(附樣例:表單數據、json、數組、對象)
 
(4)如果傳遞的參數有前綴,且前綴與接收實體類的名稱相同,那么參數也是可以正常傳遞的:
原文:SpringBoot - 獲取POST請求參數詳解(附樣例:表單數據、json、數組、對象)

(5)如果一個 get 請求的參數分屬不同的對象,也可以使用多個對象來接收參數:
1
2
3
4
5
6
7
8
9
10
11
12
13
package  com.example.demo;
 
import  org.springframework.web.bind.annotation.PostMapping;
import  org.springframework.web.bind.annotation.RestController;
 
@RestController
public  class  HelloController {
     @PostMapping ( "/hello" )
     public  String hello(User user, Phone phone) {
         return  "name:"  + user.getName() +  "\nage:"  + user.getAge()
                 "\nnumber:"  + phone.getNumber();
     }
}
原文:SpringBoot - 獲取POST請求參數詳解(附樣例:表單數據、json、數組、對象)
 

6,使用對象接收時指定參數前綴

(1)如果傳遞的參數有前綴,且前綴與接收實體類的名稱不同相,那么參數無法正常傳遞:
原文:SpringBoot - 獲取POST請求參數詳解(附樣例:表單數據、json、數組、對象)

(2)我們可以結合 @InitBinder 解決這個問題,通過參數預處理來指定使用的前綴為 u.
    除了在 Controller 里單獨定義預處理方法外,我們還可以通過 @ControllerAdvice 結合 @InitBinder 來定義全局的參數預處理方法,方便各個 Controller 使用。具體做法參考我之前的文章:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package  com.example.demo;
 
import  org.springframework.web.bind.WebDataBinder;
import  org.springframework.web.bind.annotation.*;
 
@RestController
public  class  HelloController {
     @PostMapping ( "/hello" )
     public  String hello( @ModelAttribute ( "u" ) User user) {
         return  "name:"  + user.getName() +  "\nage:"  + user.getAge();
     }
 
     @InitBinder ( "u" )
     private  void  initBinder(WebDataBinder binder) {
         binder.setFieldDefaultPrefix( "u." );
     }
}

(3)重啟程序再次發送請求,可以看到參數已經成功接收了:
原文:SpringBoot - 獲取POST請求參數詳解(附樣例:表單數據、json、數組、對象)

二、接收字符串文本數據

(1)如果傳遞過來的是 Text 文本,我們可以通過 HttpServletRequest 獲取輸入流從而讀取文本內容。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package  com.example.demo;
 
import  org.springframework.web.bind.annotation.PostMapping;
import  org.springframework.web.bind.annotation.RestController;
 
import  javax.servlet.ServletInputStream;
import  javax.servlet.http.HttpServletRequest;
import  java.io.IOException;
 
@RestController
public  class  HelloController {
     @PostMapping ( "/hello" )
     public  String hello(HttpServletRequest request) {
         ServletInputStream is =  null ;
         try  {
             is = request.getInputStream();
             StringBuilder sb =  new  StringBuilder();
             byte [] buf =  new  byte [ 1024 ];
             int  len =  0 ;
             while  ((len = is.read(buf)) != - 1 ) {
                 sb.append( new  String(buf,  0 , len));
             }
             System.out.println(sb.toString());
             return  "獲取到的文本內容為:"  + sb.toString();
         catch  (IOException e) {
             e.printStackTrace();
         finally  {
             try  {
                 if  (is !=  null ) {
                     is.close();
                 }
             catch  (IOException e) {
                 e.printStackTrace();
             }
         }
         return  null ;
     }
}

(2)下面是一個簡單的測試樣例:
原文:SpringBoot - 獲取POST請求參數詳解(附樣例:表單數據、json、數組、對象)

三、接收 JSON 數據

1,使用 Map 來接收數據

(1)如果把 json 作為參數傳遞,我們可以使用 @requestbody 接收參數,將數據轉換 Map:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package  com.example.demo;
 
import  org.springframework.web.bind.annotation.PostMapping;
import  org.springframework.web.bind.annotation.RequestBody;
import  org.springframework.web.bind.annotation.RestController;
 
import  java.util.Map;
 
@RestController
public  class  HelloController {
     @PostMapping ( "/hello" )
     public  String hello( @RequestBody  Map params) {
         return  "name:"  + params.get( "name" ) +  "\n age:"  + params.get( "age" );
     }
}

(2)下面是一個簡單的測試樣例:
原文:SpringBoot - 獲取POST請求參數詳解(附樣例:表單數據、json、數組、對象)

2,使用 Bean 對象來接收數據

(1)如果把 json 作為參數傳遞,我們可以使用 @requestbody 接收參數,將數據直接轉換成對象:
1
2
3
4
5
6
7
8
9
10
11
12
13
package  com.example.demo;
 
import  org.springframework.web.bind.annotation.PostMapping;
import  org.springframework.web.bind.annotation.RequestBody;
import  org.springframework.web.bind.annotation.RestController;
 
@RestController
public  class  HelloController {
     @PostMapping ( "/hello" )
     public  String hello( @RequestBody  User user){
         return  user.getName() +  " "  + user.getAge();
     }
}

(2)User 類定義如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package  com.example.demo;
 
public  class  User {
     private  String name;
     private  Integer age;
 
     public  String getName() {
         return  name;
     }
 
     public  void  setName(String name) {
         this .name = name;
     }
 
     public  Integer getAge() {
         return  age;
     }
 
     public  void  setAge(Integer age) {
         this .age = age;
     }
}

(3)下面是一個簡單的測試樣例:
原文:SpringBoot - 獲取POST請求參數詳解(附樣例:表單數據、json、數組、對象)
(4)如果傳遞的 JOSN 數據是一個數組也是可以的,Controller 做如下修改:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package  com.example.demo;
 
import  org.springframework.web.bind.annotation.PostMapping;
import  org.springframework.web.bind.annotation.RequestBody;
import  org.springframework.web.bind.annotation.RestController;
 
import  java.util.List;
 
@RestController
public  class  HelloController {
     @PostMapping ( "/hello" )
     public  String hello( @RequestBody  List<User> users){
         String result =  "" ;
         for (User user:users){
             result += user.getName() +  " "  + user.getAge() +  "\n" ;
         }
         return  result;
     }
}
原文:SpringBoot - 獲取POST請求參數詳解(附樣例:表單數據、json、數組、對象)
 
 
 

POSTMAN報錯信息:

{
    "msg": "Content type 'text/plain;charset=UTF-8' not supported",
    "code": 500
}

解決辦法:

疏忽大意了,需要在postman中切換下數據類型。

 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM