zoukankan      html  css  js  c++  java
  • 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>

    运行结果:

    愿你走出半生,归来仍是少年!
  • 相关阅读:
    linux 安装jdk
    linux 安装 Redis
    jvisualvm监控远程jvm的两种连接方式
    list转map(JDK8-Lambda表达式)
    循环删除list中的某一元素的三种方式
    SpringBoot-@ControllerAdvice 拦截异常并统一处理
    Spring-@ControllerAdvice 拦截异常并统一处理
    Tomcat系列
    thinkPHP5.1模型User设计
    uniapp后台api设计(微信user表)
  • 原文地址:https://www.cnblogs.com/hujunwei/p/13753197.html
Copyright © 2011-2022 走看看