一、场景
在导出的时候,只能发送get请求,但是往往我们会遇到很多查询条件,这时候使用json格式的参数,往往会使问题变得更简单。
Demo案例:
请求(真实请求):http://localhost:8080/hello/getUrlParam?user={"name":"jj","age":22}
请求(转义后):http://localhost:8080/hello/getUrlParam?user=%7b%22name%22:%22jj%22,%22age%22:22%7d
注意:引号可以不转义,只转义大括号就行
http://localhost:8080/hello/getUrlParam?user=%7b"name":"jj","age":22%7d,也可以正常请求
后台代码:
1.实体类
@Data public class Person { private String name; private String age; private String sex; }
2.controller
@RestController @RequestMapping("/hello") public class Hello { @GetMapping("/getUrlParam") public User getUrlParam(@RequestParam("user") String user){ User user1 = JSONObject.parseObject(user, User.class); return user1; } }
#注意:
@RequestParam("user"),当url中的参数名和后台不一致时使用,此处可以省略
3.pom
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.73</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
运行结果: