Quickly launch http requests in projects
I. Adding Dependency
<dependency> <groupId>cn.gjing</groupId> <artifactId>tools-httpclient</artifactId> <version>1.0.2</version> </dependency>
II. Project Use
1. HTTP request (GET case)
public static void main(String[] args) { HttpClient client = new HttpClient(); Map<String, String> map = new HashMap<>(16); map.put("a", "parameter a"); map.put("b", "parameter b"); //Send http requests String result = client.get("http://127.0.0.1:8080/test", map, String.class); System.out.println(result); }
2. HTTPS request (POST case)
public static void main(String[] args) { HttpClient client = new HttpClient(); Map<String, String> map = new HashMap<>(16); map.put("a", "parameter a"); map.put("b", "parameter b"); //Send https requests Map result = client.post("https://127.0.0.1:8080/test", map, Map.class); System.out.println(result.toString()); }
3. DELETE Case
public static void main(String[] args) { HttpClient client = new HttpClient(); ResultBean result = client.delete("http://127.0.0.1:8080/test/1", ResultBean.class); System.out.println(result.toString()); }
4. PUT case
public static void main(String[] args) { HttpClient client = new HttpClient(); Integer result = client.put("https://127.0.0.1:8080/test/2", Integer.class); System.out.println(result); }
HttpClient parameter description
null can be transmitted directly to any parameter other than the required parameter when it is not needed.
parameter | describe |
---|---|
url | Request address, must fill in |
queryMap | Request parameters |
headers | Request header |
connectTimeout | Request timeout, default 5s |
readTimeout | Read timeout, default 10 seconds |
responseType | Return type of response result, required |
jsonEntity | Json object, Json string corresponding object or map |
jsonStr | Json string |
It is better to set the return type of the response result to be consistent with the target method, otherwise the conversion exception may occur, as follows:
- Target method
@GetMapping("/test") public Integer test() { return 1111; }
- Caller
public static void main(String[] args) { HttpClient client = new HttpClient(); Map result = client.get("http://127.0.0.1:8080/test", Map.class); System.out.println(result.toString()); }
Because the target method returns an Integer type, not a key or value type, a conversion exception occurs when the caller receives using Map.
IV. UrlUtil Tool Class
1,urlAppend
Url splicing, return the result format such as: http://xxx/param1/param2
public static void main(String[] args) { String url = "http://127.0.0.1:8080/"; Object[] param = {1, 2, 3, 4}; UrlUtil.urlAppend(url, param); }
2,paramUnicodeSort
Parameters are sorted by Unicode code code of field names from small to large (dictionary order), and the result format is as follows: a = parameter 1 & B = parameter 2
public static void main(String[] args) { Map<String, Object> map = new HashMap<>(16); map.put("a", "Parameter 1"); map.put("b", "Parameter 2"); UrlUtil.paramUnicodeSort(map, false, false); }
Parameter description
parameter | describe |
---|---|
paramMap | parameter |
urlEncode | Do you carry out URL coding? |
keyToLower | Whether the key value of the converted parameter should be lowercase |
3,urlParamToMap
Converting the parameters after the URL address to a map
public static void main(String[] args) { String url = "http://127.0.0.1:8080?a=2&b=2"; UrlUtil.urlParamToMap(url); }
Project source code can be viewed at GitHUb: tools-httpclient