9
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

springのUriComponentsBuilderでクエリパラメータをエンコード

Last updated at Posted at 2018-04-02

環境

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.RELEASE</version>
	</parent>

エンコードの指示

encodeでエンコードを指示する。

URI target = UriComponentsBuilder.newInstance()
        .scheme("http")
        .host("example.com")
        .queryParam("foo", "b&r")
        .build()
        .encode()
        .toUri();
System.out.println(target);// http://example.com?foo=b%26r

エンコード済みのURLでfromUriを使う場合はbuild(boolean encoded)trueを指定する。

URI source = new URI("http://example.com?foo=b%26r");
URI target = UriComponentsBuilder
        .fromUri(source)
        .build(true)
        .toUri();
System.out.println(target);// http://example.com?foo=b%26r

ハマった点

もしencodeを指定しない場合、エンコードは行われない。

URI target = UriComponentsBuilder.newInstance()
        .scheme("http")
        .host("example.com")
        .queryParam("foo", "b&r")
        .build()
        .toUri();
System.out.println(target); // http://example.com?foo=b&r

もしfromUriでエンコード済みのURLを渡し、かつ、build(boolean encoded)trueを指定しない場合、エンコードが行われる。よって、結果的に二重にエンコードが行われる。

URI source = new URI("http://example.com?foo=b%26r");
URI target = UriComponentsBuilder
        .fromUri(source)
        .build()
        .toUri();
System.out.println(target);// http://example.com?foo=b%2526r

参考

[SPR-14256] Doc: UriComponentsBuilder does not encode query parameters - Spring JIRA

9
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?