原文网址:SpringBoot--获取路径中的参数(x-www-form-urlencoded)--方法/实例_IT利刃出鞘的博客-CSDN博客
简介
本文用示例介绍SpringMVC如何获取路径中的参数。也就是: Content-Type为x-www-form-urlencoded。
代码
Controller
BasicController.java
package com.example.demo.controller;
import com.example.demo.entity.User;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
// localhost:8080/basic/no_annotation/string1
@RequestMapping("/basic")
@RestController
public class BasicController {
@RequestMapping("/1")
public String test1(int intVal, Long longVal, String myStr) {
System.out.println("intVal : " + intVal);
System.out.println("longVal : " + longVal);
System.out.println("myStr : " + myStr);
return "Hello world";
}
@RequestMapping("/2")
public User test2(@RequestParam Integer intVal,
@RequestParam(value = "my_str", required = false) String[] myStr,
@RequestParam String[] password,
@RequestParam List<Integer> scoreArray,
@RequestParam Integer age,
User user) {
System.out.println("intVal : " + intVal);
System.out.println("age : " + age);
System.out.println("myStr : " + Arrays.asList(myStr));
System.out.println("password : " + Arrays.asList(password));
System.out.println("scoreArray : " + scoreArray);
return user;
}
@RequestMapping("/3")
public User test3(@RequestBody User user) {
return user;
}
}
Entity
User.java
package com.example.demo.entity;
import lombok.Data;
import java.util.List;
@Data
public class User {
private String name;
private Integer age;
private String[] password;
private List<Integer> scoreArray;
}
Account.java
package com.example.demo.entity;
import lombok.Data;
import java.io.Serializable;
@Data
public class Account implements Serializable {
private String phoneNum;
private String[] emails;
}
测试
测试1:基本类型为空导致错误
http://localhost:8080/basic/1
后端打印
java.lang.IllegalStateException: Optional int parameter 'intVal' is present but cannot be translated into a null value due to being declared as a primitive type. Consider declaring it as object wrapper for the corresponding primitive type.
前端结果
测试2:正常操作
其实也不正常,最好不要用基本类型接收。
http://localhost:8080/basic/1?intVal=2&myStr=hehe
后端
intVal : 2
longVal : null
myStr : hehe
前端
测试3:正常操作
http://localhost:8080/basic/2?intVal=2&my_str=Tony,Stark&name=Jarvis&age=21&password=ab,cd&scoreArray=99,98&phoneNum=12
后端
intVal : 2
age : 21
myStr : [Tony, Stark]
password : [ab, cd]
scoreArray : [99, 98]
前端
结论
- 如果一个参数,实体类与接口参数都有,则都会赋值。
- 若实体类中有数组/List 成员,也会对它直接赋值。
- 若实体类含有实体类,无法赋值。因为参数中没有与实体类相同名字的key,此时就要用form-data或者json了。(或者x-www-urlencoded也有写类参数的方法,目前不知道)
测试4:@RequestParam缺少参数访问(失败)
http://localhost:8080/basic/2?intVal=2&my_str=Tony,Stark&name=Jarvis&age=21&password=ab,cd&phoneNum=12
后端
2020-07-04 18:00:37.222 WARN 15844 --- [nio-8080-exec-8] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required List parameter 'scoreArray' is not present]
前端
测试5:用@RequestBody接收(失败)
http://localhost:8080/basic/3?intVal=2&my_str=Tony,Stark&name=Jarvis&age=21&password=ab,cd&scoreArray=99,98&phoneNum=12
后端
2020-07-04 14:02:55.954 WARN 15844 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public com.example.demo.entity.User com.example.demo.controller.BasicController.test3(com.example.demo.entity.User)]
前端