当前位置: 首页 > 知识库问答 >
问题:

Spring boot TestRestTemplate不对请求参数值进行URL解码

卜鹏
2023-03-14

设置:

  • springboot应用程序;用于测试的Spring Boot测试TestRest模板

如果启动服务器并使用URL编码的值对endpoint进行卷曲,则会在响应中获得非URL编码的值。

如果我用TestRestTemplate查询完全相同的东西,foo会得到一个URL编码的值来处理,而在现实世界中事情开始出错。。。

com.example.应用

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class App {

    @RequestMapping(path = "/test")
    public String foo(@RequestParam("q") String val) {
        return val;
    }

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

通用域名格式。实例特斯塔普

package com.example;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class TestApp {

    @Autowired
    private TestRestTemplate rest;

    @Test
    public void testIt() {
        URI uri = UriComponentsBuilder.fromPath("/test")
            .queryParam("q", BIG_STR)
            .build().encode().toUri();
        System.out.println("URI: " + uri);
        ResponseEntity<String> res = rest.getForEntity(uri, String.class);
        System.out.println("resBody: " + res.getBody());
    }

    private static String BIG_STR = "[ { \"_id\": \"58ac70a1d998ec63dc004158\", \"index\": 0, \"guid\": \"68124ce4-6a65-4459-8189-f245236399a9\", \"isActive\": true, \"balance\": \"$3,415.67\", \"picture\": \"http://placehold.it/32x32\", \"age\": 36, \"eyeColor\": \"brown\", \"name\": \"Bobbi Barr\", \"gender\": \"female\", \"company\": \"ACCUPHARM\", \"email\": \"bobbibarr@accupharm.com\", \"phone\": \"+1 (938) 592-3835\", \"address\": \"551 Lester Court, Sedley, South Carolina, 3698\", \"about\": \"Dolor deserunt nostrud ipsum sunt aute elit exercitation tempor eu ipsum. Dolor magna nisi incididunt sit non mollit ut aliqua. Magna est officia veniam sit deserunt eiusmod consectetur do dolore et ea aliquip. Magna velit tempor est dolore sint voluptate. Et enim cillum voluptate nisi est non. Non enim amet ad qui cillum ipsum ipsum magna id nostrud cillum amet esse.\\r\\n\", \"registered\": \"2014-08-05T12:07:38 +07:00\", \"latitude\": -79.673386, \"longitude\": -30.821609, \"tags\": [ \"aute\", \"consequat\", \"veniam\", \"exercitation\", \"quis\", \"proident\", \"mollit\" ], \"friends\": [ { \"id\": 0, \"name\": \"Carolina Barton\" }, { \"id\": 1, \"name\": \"Glover Gibbs\" }, { \"id\": 2, \"name\": \"Simpson Hicks\" } ], \"greeting\": \"Hello, Bobbi Barr! You have 2 unread messages.\", \"favoriteFruit\": \"banana\" } ]";
}

建筑格拉德尔

apply plugin: 'java'

repositories {
  mavenCentral()
}

dependencies {
  compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.1.RELEASE'

  testCompile group: 'junit', name: 'junit', version: '4.+'
  testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '1.5.1.RELEASE'
}

服务器启动卷的输出:

curl -i "http://localhost:8080/test?q=%5B%20%7B%20%22_id%22:%20%2258ac70a1d998ec63dc004158%22,%20%22index%22:%200,%20%22guid%22:%20%2268124ce4-6a65-4459-8189-f245236399a9%22,%20%22isActive%22:%20true,%20%22balance%22:%20%22$3,415.67%22,%20%22picture%22:%20%22http://placehold.it/32x32%22,%20%22age%22:%2036,%20%22eyeColor%22:%20%22brown%22,%20%22name%22:%20%22Bobbi%20Barr%22,%20%22gender%22:%20%22female%22,%20%22company%22:%20%22ACCUPHARM%22,%20%22email%22:%20%22bobbibarr@accupharm.com%22,%20%22phone%22:%20%22%2B1%20(938)%20592-3835%22,%20%22address%22:%20%22551%20Lester%20Court,%20Sedley,%20South%20Carolina,%203698%22,%20%22about%22:%20%22Dolor%20deserunt%20nostrud%20ipsum%20sunt%20aute%20elit%20exercitation%20tempor%20eu%20ipsum.%20Dolor%20magna%20nisi%20incididunt%20sit%20non%20mollit%20ut%20aliqua.%20Magna%20est%20officia%20veniam%20sit%20deserunt%20eiusmod%20consectetur%20do%20dolore%20et%20ea%20aliquip.%20Magna%20velit%20tempor%20est%20dolore%20sint%20voluptate.%20Et%20enim%20cillum%20voluptate%20nisi%20est%20non.%20Non%20enim%20amet%20ad%20qui%20cillum%20ipsum%20ipsum%20magna%20id%20nostrud%20cillum%20amet%20esse.%5Cr%5Cn%22,%20%22registered%22:%20%222014-08-05T12:07:38%20%2B07:00%22,%20%22latitude%22:%20-79.673386,%20%22longitude%22:%20-30.821609,%20%22tags%22:%20%5B%20%22aute%22,%20%22consequat%22,%20%22veniam%22,%20%22exercitation%22,%20%22quis%22,%20%22proident%22,%20%22mollit%22%20%5D,%20%22friends%22:%20%5B%20%7B%20%22id%22:%200,%20%22name%22:%20%22Carolina%20Barton%22%20%7D,%20%7B%20%22id%22:%201,%20%22name%22:%20%22Glover%20Gibbs%22%20%7D,%20%7B%20%22id%22:%202,%20%22name%22:%20%22Simpson%20Hicks%22%20%7D%20%5D,%20%22greeting%22:%20%22Hello,%20Bobbi%20Barr20You%20have%202%20unread%20messages.%22,%20%22favoriteFruit%22:%20%22banana%22%20%7D%20%5D"
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 1194
Date: Tue, 21 Feb 2017 17:19:26 GMT

[ { "_id": "58ac70a1d998ec63dc004158", "index": 0, "guid": "68124ce4-6a65-4459-8189-f245236399a9", "isActive": true, "balance": ",415.67", "picture": "http://placehold.it/32x32", "age": 36, "eyeColor": "brown", "name": "Bobbi Barr", "gender": "female", "company": "ACCUPHARM", "email": "bobbibarr@accupharm.com", "phone": "+1 (938) 592-3835", "address": "551 Lester Court, Sedley, South Carolina, 3698", "about": "Dolor deserunt nostrud ipsum sunt aute elit exercitation tempor eu ipsum. Dolor magna nisi incididunt sit non mollit ut aliqua. Magna est officia veniam sit deserunt eiusmod consectetur do dolore et ea aliquip. Magna velit tempor est dolore sint voluptate. Et enim cillum voluptate nisi est non. Non enim amet ad qui cillum ipsum ipsum magna id nostrud cillum amet esse.\r\n", "registered": "2014-08-05T12:07:38 +07:00", "latitude": -79.673386, "longitude": -30.821609, "tags": [ "aute", "consequat", "veniam", "exercitation", "quis", "proident", "mollit" ], "friends": [ { "id": 0, "name": "Carolina Barton" }, { "id": 1, "name": "Glover Gibbs" }, { "id": 2, "name": "Simpson Hicks" } ], "greeting": "Hello, Bobbi Barr20You have 2 unread messages.", "favoriteFruit": "banana" } ]

集成测试的日志输出:

2017-02-21 09:28:17.912  INFO 51360 --- [           main] com.example.TestApp                      : Started TestApp in 2.016 seconds (JVM running for 2.598)
URI: /test?q=%5B%20%7B%20%22_id%22:%20%2258ac70a1d998ec63dc004158%22,%20%22index%22:%200,%20%22guid%22:%20%2268124ce4-6a65-4459-8189-f245236399a9%22,%20%22isActive%22:%20true,%20%22balance%22:%20%22$3,415.67%22,%20%22picture%22:%20%22http://placehold.it/32x32%22,%20%22age%22:%2036,%20%22eyeColor%22:%20%22brown%22,%20%22name%22:%20%22Bobbi%20Barr%22,%20%22gender%22:%20%22female%22,%20%22company%22:%20%22ACCUPHARM%22,%20%22email%22:%20%22bobbibarr@accupharm.com%22,%20%22phone%22:%20%22%2B1%20(938)%20592-3835%22,%20%22address%22:%20%22551%20Lester%20Court,%20Sedley,%20South%20Carolina,%203698%22,%20%22about%22:%20%22Dolor%20deserunt%20nostrud%20ipsum%20sunt%20aute%20elit%20exercitation%20tempor%20eu%20ipsum.%20Dolor%20magna%20nisi%20incididunt%20sit%20non%20mollit%20ut%20aliqua.%20Magna%20est%20officia%20veniam%20sit%20deserunt%20eiusmod%20consectetur%20do%20dolore%20et%20ea%20aliquip.%20Magna%20velit%20tempor%20est%20dolore%20sint%20voluptate.%20Et%20enim%20cillum%20voluptate%20nisi%20est%20non.%20Non%20enim%20amet%20ad%20qui%20cillum%20ipsum%20ipsum%20magna%20id%20nostrud%20cillum%20amet%20esse.%5Cr%5Cn%22,%20%22registered%22:%20%222014-08-05T12:07:38%20%2B07:00%22,%20%22latitude%22:%20-79.673386,%20%22longitude%22:%20-30.821609,%20%22tags%22:%20%5B%20%22aute%22,%20%22consequat%22,%20%22veniam%22,%20%22exercitation%22,%20%22quis%22,%20%22proident%22,%20%22mollit%22%20%5D,%20%22friends%22:%20%5B%20%7B%20%22id%22:%200,%20%22name%22:%20%22Carolina%20Barton%22%20%7D,%20%7B%20%22id%22:%201,%20%22name%22:%20%22Glover%20Gibbs%22%20%7D,%20%7B%20%22id%22:%202,%20%22name%22:%20%22Simpson%20Hicks%22%20%7D%20%5D,%20%22greeting%22:%20%22Hello,%20Bobbi%20Barr!%20You%20have%202%20unread%20messages.%22,%20%22favoriteFruit%22:%20%22banana%22%20%7D%20%5D
2017-02-21 09:28:18.002  INFO 51360 --- [o-auto-1-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-02-21 09:28:18.002  INFO 51360 --- [o-auto-1-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2017-02-21 09:28:18.013  INFO 51360 --- [o-auto-1-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 11 ms
resBody: %5B%20%7B%20%22_id%22:%20%2258ac70a1d998ec63dc004158%22,%20%22index%22:%200,%20%22guid%22:%20%2268124ce4-6a65-4459-8189-f245236399a9%22,%20%22isActive%22:%20true,%20%22balance%22:%20%22$3,415.67%22,%20%22picture%22:%20%22http://placehold.it/32x32%22,%20%22age%22:%2036,%20%22eyeColor%22:%20%22brown%22,%20%22name%22:%20%22Bobbi%20Barr%22,%20%22gender%22:%20%22female%22,%20%22company%22:%20%22ACCUPHARM%22,%20%22email%22:%20%22bobbibarr@accupharm.com%22,%20%22phone%22:%20%22%2B1%20(938)%20592-3835%22,%20%22address%22:%20%22551%20Lester%20Court,%20Sedley,%20South%20Carolina,%203698%22,%20%22about%22:%20%22Dolor%20deserunt%20nostrud%20ipsum%20sunt%20aute%20elit%20exercitation%20tempor%20eu%20ipsum.%20Dolor%20magna%20nisi%20incididunt%20sit%20non%20mollit%20ut%20aliqua.%20Magna%20est%20officia%20veniam%20sit%20deserunt%20eiusmod%20consectetur%20do%20dolore%20et%20ea%20aliquip.%20Magna%20velit%20tempor%20est%20dolore%20sint%20voluptate.%20Et%20enim%20cillum%20voluptate%20nisi%20est%20non.%20Non%20enim%20amet%20ad%20qui%20cillum%20ipsum%20ipsum%20magna%20id%20nostrud%20cillum%20amet%20esse.%5Cr%5Cn%22,%20%22registered%22:%20%222014-08-05T12:07:38%20%2B07:00%22,%20%22latitude%22:%20-79.673386,%20%22longitude%22:%20-30.821609,%20%22tags%22:%20%5B%20%22aute%22,%20%22consequat%22,%20%22veniam%22,%20%22exercitation%22,%20%22quis%22,%20%22proident%22,%20%22mollit%22%20%5D,%20%22friends%22:%20%5B%20%7B%20%22id%22:%200,%20%22name%22:%20%22Carolina%20Barton%22%20%7D,%20%7B%20%22id%22:%201,%20%22name%22:%20%22Glover%20Gibbs%22%20%7D,%20%7B%20%22id%22:%202,%20%22name%22:%20%22Simpson%20Hicks%22%20%7D%20%5D,%20%22greeting%22:%20%22Hello,%20Bobbi%20Barr!%20You%20have%202%20unread%20messages.%22,%20%22favoriteFruit%22:%20%22banana%22%20%7D%20%5D

知道怎么回事吗?

共有1个答案

储俊英
2023-03-14

你正在击中这个错误。您可以通过使用String而不是URI调用TestRestTem板来避免这个问题。

 类似资料:
  • 我试图通过参数在谷歌搜索,它的工作,当我搜索一个词,但一个我做空间它坏了我知道有一种方法来编码网址。 回溯: 还有一次,我用希伯来文写道: UnicodeEncodeError:“ascii”编解码器无法对位置14-18中的字符进行编码:序号不在范围内(128)

  • 问题内容: 我正在尝试通过GET请求中的URL参数传递API密钥。 但是,我注意到在发送请求时,Axios会在我的API密钥中对字符进行编码。这会导致API拒绝我的请求,因为它无法识别我的密钥。 如何防止Axios对我的GET参数进行编码? 问题答案: 您可以使用自定义参数序列化器,如下所示: 可以在实例级别设置: 或在全球范围内: 另一个选择是将api键直接添加到URL: 您可以使用`param

  • jQuery获取地址栏url参数 获取url参数内容

  • 我使用Jersey和嵌入式Tomcat开发了REST服务器。 当我的客户端使用Jersey向我的服务器发送请求并添加其值为UTF-8编码的请求参数时,我会收到服务器端解码的参数。 我的问题是,谁负责解码这个参数?是雄猫吗?球衣<我可以禁用此功能吗?

  • 可以在模板中直接使用$Request对象,直接输入它的属性或调用它的大部分方法,但只支持方法的第一个参数; // 调用Request对象的get方法 传入参数为id {$Request.get.id} // 调用Request对象的param方法 传入参数为name {$Request.param.name} // 调用Request对象的param方法 传入参数为post.post_title

  • 可以在模板中直接使用$Request对象,直接输入它的属性或调用它的大部分方法,但只支持方法的第一个参数; // 调用Request对象的get方法 传入参数为id {$Request.get.id} // 调用Request对象的param方法 传入参数为name {$Request.param.name} // 调用Request对象的param方法 传入参数为post.post_title