[JAVA] Pitfalls of WebTarget.queryParam () in JAX-RS

Note that I was addicted to the specification of WebTarget.queryParam () of JAX-RS Client.

problem

The specification of WebTarget.queryParam () is [here](https://docs.oracle.com/javaee/7/api/javax/ws/rs/client/WebTarget.html#queryParam-java.lang.String-java. lang.Object ...-). It is a method to set query parameters for WebTarget.

There are some caveats about this method and URI encoding.

The URI template is a function that inserts the path parameter into the part surrounded by {}, but if you think about it, there is a slight problem. The problem is that the query parameter value cannot contain "{" or "}" itself. This is because both "{" and "}" are interpreted as path parameters.

To avoid this, "{" and "}" must be URI-encoded. It means that it should be% 7b and% 7d, respectively.

However, in implementations such as Jersey and RESTEasy, this method performs URI-encoding as described above, so if it is URI-encoded again as it is, it should be converted to% 257b and% 257d. In other words, if you can't use {or}, ...?

Workaround

The Jersey implementation avoids this by avoiding double URI encoding. Specifically, the rule is that% is not encoded if there are two consecutive hexadecimal characters (0-9a-fA-F) after%. So it's okay to encode {,} as it is.

Conversely, if you happen to have two consecutive hexadecimal characters after%,% will not be URL-encoded.

Therefore, you should always ** always URI-encode yourself before calling queryParam () **.

I don't know what the other implementations are doing. At first glance at the RESTEasy code, it seems that% was not encoded at all.

Of course, the above behavior cannot be read from the JAX-RS specification (implementation dependent). So let's check the operation before using it.

Recommended Posts

Pitfalls of WebTarget.queryParam () in JAX-RS
[Spring] Pitfalls of BeanUtils.copyProperties
pitfalls of nextInt () → nextLine ()
Subtle pitfalls of lombok
Pitfalls of Active Hash
Output in multiples of 3
Implementation of gzip in java
Judgment of fractions in Ruby
Implementation of tri-tree in Java
Implementation of HashMap in kotlin
Order of route evaluation in Spark
Introduction of javaFX in Eclipse (April 2020)
Basics of sending Gmail in Ruby
Differences in split methods of StringUtils
Implementation of asynchronous processing in Tomcat
List of members added in Java 9
List of types added in Java 9
Order of processing in the program
Implementation of like function in Java
Number of digits in numeric item
Types of exceptions in business systems