简体   繁体   中英

How to encode path parameter using Java Jersey

How do you encode a path parameter (not form-url-encoded) but just a single URL that's appended in the format:

public String method(@PathParam("url") String url) {
}

There are lots of references to form URL encoding, but I want to simply encode a string as in the above.

Like mentioned in the previous answer URLEncoder can only be used for query paramaters, not path parameters. This matters eg for spaces which are a + in the query parameter but a %20 in the path.

 org.springframework.web.util.UriUtils.encodePath()

can be used. Also using an org.apache.http.client.utils.URIBuilder would work. setPath is escaping the path part here. Also pure Java by using a constructor of java.net.Uri works.

Why would you want to *en*code it there, if anything wouldn't you want to *de*code it? In any case, you would call the standard URLEncoder .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM