• SpringBoot系列---【get请求如何传递json对象的参数?】


    一、场景

    在导出的时候,只能发送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>

    运行结果:

    愿你走出半生,归来仍是少年!
  • 相关阅读:
    strpos与strstr之间的区别
    jquery 滚动效果插件
    自定义加密解密函数
    access变转换为mysql表工具
    CI学习总结
    表单验证
    解决国外模板h1、h2、h3...不显示中文文章标题的问题
    leetcode Largest Rectangle in Histogram
    leetcode Remove Duplicates from Sorted List
    leetcode[82] Remove Duplicates from Sorted List II
  • 原文地址:https://www.cnblogs.com/hujunwei/p/13753197.html
Copyright © 2020-2023  润新知