https://github.com/emilianbold/uriencoder
Standalone UriComponentsBuilder from spring
https://github.com/emilianbold/uriencoder
Last synced: 2 months ago
JSON representation
Standalone UriComponentsBuilder from spring
- Host: GitHub
- URL: https://github.com/emilianbold/uriencoder
- Owner: emilianbold
- Created: 2019-10-16T17:33:11.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-10-13T16:48:18.000Z (over 4 years ago)
- Last Synced: 2025-03-23T22:11:52.534Z (2 months ago)
- Language: Java
- Homepage:
- Size: 156 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# uriencoder: Properly encode your URL
Based on https://github.com/spring-projects/spring-framework/commit/d1aee0e8691c41753621332ff69b17be3f7c8ba2 from Oct 6th.
spring-web has many good utils but is too big and drags in many other dependencies...
To encode your URL, and particularly your query parameters, you just need a small method from UriComponentsBuilder.
Extracted now in SpringUtil.How to use:
```java
String base = "http://example.com/query";Map param = new TreeMap<>();
param.put("msg", "hello world");
param.put("id", "&<>");String query = param.entrySet().stream()
.map((e) -> e.getKey() + "=" + SpringUtil.encodeUriComponent(e.getValue(), StandardCharsets.UTF_8, SpringUtil.Type.QUERY_PARAM))
.collect(Collectors.joining("&"));String url = base + "?" + query;
Assert.assertEquals("http://example.com/query?id=%26%3C%3E&msg=hello%20world", url);
```