How to use getRequestUri().getRawQuery() in the url on Java escape

As know url would not allow some special character there, so need encode for that:
: metadata=[{name: serialnumber, value: aaaaaaaaa},{name: register, value: abcde}] in the url

I tried this encode would work

String abc= java.net.URLEncoder.encode("http://localhost:9080/myapp/myapp/search?metadata=[{name: serialnumber, value: aaaaaaaaa},{name: register, value: abcde}]", "utf-8");

But why would be fail following if use info.getRequestUri().getRawQuery() instead?

public Response search(@Context final UriInfo info, @Context final HttpHeaders httpHeaders) throws Exception {
        String requestURI = java.net.URLEncoder.encode(info.getRequestUri().getRawQuery(), "utf-8");

error:

Caused by: java.net.URISyntaxException: Illegal character in query

How can I encode this successfully if I will use info.getRequestUri().getRawQuery()

This might be due to the white space in metadata.
You may either –

  1. Remove the white spaces (metadata=[{name:serialnumber,value:aaaaaaaaa},{name:register,value:abcde}]
  2. replace white spaces with +
    as mentioned in this question

Hope that work!