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; } } |
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; } } |
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" ); } } |
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; } } |
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(); } } |
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; } } |
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(); } } |
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." ); } } |
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 ; } } |
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" ); } } |
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(); } } |
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; } } |
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; } } |
POSTMAN报错信息:
{ "msg": "Content type ‘text/plain;charset=UTF-8‘ not supported", "code": 500 }
解决办法:
疏忽大意了,需要在postman中切换下数据类型。
SpringBoot - 获取POST请求参数详解(附样例:表单数据、json、数组、对象)
原文:https://www.cnblogs.com/netcorner/p/13544443.html