带有查询参数并使用UriComponentsBuilder中的"+"。

huangapple go评论88阅读模式
英文:

Having queryParam with "+" using UriComponentsBuilder

问题

根据问题 https://stackoverflow.com/questions/36135344/springs-uricomponentsbuilder-queryparam-issue,如果查询参数的值中有加号("+"),默认情况下加号不会被编码。为什么会这样,我如何强制进行编码呢?

在我的示例中,我想要使用 ISO 编码的时间戳,并且我需要对加号进行编码,因为另一个端点会将 ""+"" 解释为空格 (" "),从而破坏时间戳。

UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("https://example.com/api/")//
        .queryParam("date1", "2020-10-05T08:20:00.000+02:00") //
        .query("date2=2020-10-05T08:20:00.000+02:00");

System.out.println(builder.build().toString());
// 生成 https://example.com/api/?date1=2020-10-05T08:20:00.000+02:00&date2=2020-10-05T08:20:00.000+02:00

System.out.println(builder.build().encode().toString());
// 生成 https://example.com/api/?date1=2020-10-05T08:20:00.000+02:00&date2=2020-10-05T08:20:00.000+02:00

预期的结果应该像这样对加号进行编码:2020-10-05T08%3A20%3A00.000%2B02%3A00,或者至少使用 %2B 编码为 2020-10-05T08:20:00.000%2B02:00

英文:

Based on question https://stackoverflow.com/questions/36135344/springs-uricomponentsbuilder-queryparam-issue, what if a query parameter has a plus ("+") in its value? By default, a plus will not encoded. Why and how can I force this?

In my example, I want to use da ISO-coded timestamp and I need to encode the plus, because the other endpoint will interprete the "+" as a space (" ") and this breaks by timestamp.

UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("https://example.com/api/")//
        .queryParam("date1", "2020-10-05T08:20:00.000+02:00") //
        .query("date2=2020-10-05T08:20:00.000+02:00");

System.out.println(builder.build().toString());
// produces https://example.com/api/?date1=2020-10-05T08:20:00.000+02:00&date2=2020-10-05T08:20:00.000+02:00

System.out.println(builder.build().encode().toString());
// produces https://example.com/api/?date1=2020-10-05T08:20:00.000+02:00&date2=2020-10-05T08:20:00.000+02:00

The expected result should encode the plus sign like 2020-10-05T08%3A20%3A00.000%2B02%3A00 or at least 2020-10-05T08:20:00.000%2B02:00 with %2B.

答案1

得分: 0

你可以使用DefaultUriBuilderFactory来对查询参数进行编码,如下所示:

String baseUrl = "https://example.com";
DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory(baseUrl);

String uri = uriBuilderFactory.uriString("/api/")
        .queryParam("date1", "{date1}")
        .build("2020-10-05T08:20:00.000+02:00").toString(); // https://example.com/api/?date1=2020-10-05T08%3A20%3A00.000%2B02%3A00
英文:

You can use DefaultUriBuilderFactory to encode the query values as such:

String baseUrl = "https://example.com";
		DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory(baseUrl);

		String uri = uriBuilderFactory.uriString("/api/")
				.queryParam("date1", "{date1}")
				.build("2020-10-05T08:20:00.000+02:00").toString(); // https://example.com/api/?date1=2020-10-05T08%3A20%3A00.000%2B02%3A00

</details>



huangapple
  • 本文由 发表于 2020年10月5日 17:49:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64206248.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定