How can I pass string path param containing slash character?
Reserved characters such as ,
and /
must be URL encoded.
,
is encoded as%2C
/
is encoded as%2F
Try http://ip:port/samples/2000%2C2006%2C6576%2FM982
.
The RFC 3986 defines the following set of reserved characters that can be used as delimiters. Hence, they require URL encoding:
: / ? # / [ ] / @ ! $ & ' ( ) * + , ; =
Unreserved characters do not require URL encoding:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
a b c d e f g h i j k l m n o p q r s t u v w x y z
0 1 2 3 4 5 6 7 8 9 - _ . ~
If URL encoding ,
is not a good alternative for you, you could consider using query parameters. Your code will be like:
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getSample(@QueryParam("business") String business,
@QueryParam("year") String year,
@QueryParam("sample") String sampleId {
...
}
And your URL will be like http://ip:port/samples?business=2000&year=2006&sample=6576%2FM982
.
Please note that the /
still needs to be URL encoded.
Actually, .+
helped. Just that it needs to be part of @Path
but not @PathParam
.
Example:
@Path("{sample:.+}")
Try to use {sample:.+}
instead of {sample}
The @Path
annotation is a regular expression and regex do not match /
character.
To override the regex, we can add .+
at the end of the PathParam
.
In this way we can allow /
in our path and avoid using %2F
.