Using tools-httpclient, HTTP/HTTPS requests can be sent quickly in a project. Currently, four common requests, POST.GET.DELETE.PUT, are supported, and POST is supported to use JSON parameters.
How to use the project:
- Add dependencies:
<dependency> <groupId>cn.gjing</groupId> <artifactId>tools-httpclient</artifactId> <version>1.0.0</version> </dependency>
- Use directly in the interface you need to use:
- HTTP Request (GET Case)
public static void main(String[] args) { Map<String, String> map = new HashMap<>(16); map.put("a", "parameter a"); map.put("b", "parameter b"); //Send http request String result = HttpClient.get("http://127.0.0.1:8080/test", map, String.class); System.out.println(result); }
- HTTPS Request (POST Case)
public static void main(String[] args) { Map<String, String> map = new HashMap<>(16); map.put("a", "parameter a"); map.put("b", "parameter b"); //Send https requests Map result = HttpClient.post("https://127.0.0.1:8080/test", map, Map.class); System.out.println(result.toString()); }
- DELETE Case
public static void main(String[] args) { ResultBean result = HttpClient.delete("http://127.0.0.1:8080/test/1", ResultBean.class); System.out.println(result.toString()); }
- PUT case
public static void main(String[] args) { Integer result = HttpClient.put("https://127.0.0.1:8080/test/2", Integer.class); System.out.println(result); }
HttpClient parameter description (any parameter other than the required parameter can pass null directly if it is not needed):
- url: Request address (required)
- queryMap: Request parameters
- headers: request header
- ConneTimeout: Request timeout, default 5s
- readTimeout: Read timeout, default 10 seconds
- responseType: Response result return type (required)
- body:JSON object, used when the target method parameter of the call receives a parameter using @RequestBody
tip: The response result return type is best set to match the target method, otherwise conversion exceptions may occur
Target Method
@GetMapping("/test") public Integer test() { return 1111; }
Caller
public static void main(String[] args) { Map result = HttpClient.get("http://127.0.0.1:8080/test", Map.class); System.out.println(result.toString()); }
Since the target method returns an Integer type, not a key, value type, a conversion exception occurs when the caller receives using Map.
- -
- UrlUtil Tool Class
- UrlAppend (for URL address direct reference)
public static void main(String[] args) { String url = "http://127.0.0.1:8080/"; Object[] param = {1, 2, 3, 4}; System.out.println(UrlUtil.urlAppend(url, param)); } //The output is http://127.0.0.1:8080/1/2/3/4/
- ParamUnicodeSort (parameters are sorted from smallest to largest (dictionary order) by the Unicode code code of the field name)
public static void main(String[] args) { Map<String, Object> map = new HashMap<>(16); map.put("a", "Parameter 1"); map.put("b", "Parameter 2"); /* The method takes three parameters: paramMap: parameter urlEncode: Whether to encode the URL keyToLower: Whether the key value of the converted parameter is lowercase */ System.out.println(UrlUtil.paramUnicodeSort(map, false, false)); } //The output is a=parameter 1&b=parameter 2
- UrlParamToMap (converts the parameters that follow the URL address to a map)
public static void main(String[] args) { String url = "http://127.0.0.1:8080?a=2&b=2"; System.out.println(UrlUtil.urlParamToMap(url)); } //The output is: {a=2, b=2}
- -
The project source code can be viewed at GitHUb: tools-httpclient
- -