How to force URIBuilder.path(…) to encode parameters like “

后端 未结 3 1345
自闭症患者
自闭症患者 2020-12-10 02:36

How to force URIBuilder.path(...) to encode parameters like \"%AD\"?

The methods path, replacePath and segm

3条回答
  •  情深已故
    2020-12-10 03:22

    UriBuilder

    This is possible with help of UriComponent from Jersey or URLEncoder directly from Java:

    UriBuilder.fromUri("https://dummy.com")
            .queryParam("param",
                    UriComponent.encode("%AD",
                        UriComponent.Type.QUERY_PARAM_SPACE_ENCODED))
            .build();
    

    Which result in:

    https://dummy.com/?param=%25AD
    

    Or:

    UriBuilder.fromUri("https://dummy.com")
            .queryParam("param", URLEncoder.encode("%AD", "UTF-8"))
            .build()
    

    Will result in:

    https://dummy.com/?param=%25AD
    

    For a more complex examples (i.e. encoding JSON in query param) this approach is also possible. Let's assume you have a JSON like {"Entity":{"foo":"foo","bar":"bar"}}. When encoded using UriComponent the result for query param would look like:

    https://dummy.com/?param=%7B%22Entity%22:%7B%22foo%22:%22foo%22,%22bar%22:%22bar%22%7D%7D
    

    JSON like this could be even injected via @QueryParam into resource field / method param (see JSON in Query Params or How to Inject Custom Java Types via JAX-RS Parameter Annotations).


    Which Jersey version do you use? In the tags you mention Jersey 2 but in the RuntimeDelegate section you're using Jersey 1 stuff.

提交回复
热议问题