java springboot 工具类之post、get请求
org.springblade.modules.netprobe.utilsorg.springframework.http.*org.springframework.util.CollectionUtilsorg.springframework.util.MultiValueMaporg.springframework.web.client.RestTemplatejava.util.MapRestTemplateUtil { RestTemplate =RestTemplate()<>(String urlObject entityClass<> cls){ HttpEntity<Object> request = HttpEntity<>(entity)(urlrequestcls)} <>(String urlMultiValueMap<StringObject> paramsClass<> cls){ HttpHeaders headers = HttpHeaders()headers.setContentType(MediaType.)HttpEntity<Object> request = HttpEntity<>(paramsheaders)(urlrequestcls)} <>(String urlHttpEntity<Object> requestClass<> cls){ ResponseEntity<> exchange = .exchange(urlHttpMethod.requestcls)exchange.getBody()} <>(String urlMap<StringObject> paramsClass<> cls){ StringBuilder urlBuiler=StringBuilder(url)urlBuiler.append()(!CollectionUtils.(params)){ (Map.Entry<StringObject> entry : params.entrySet()) { urlBuiler.append(entry.getKey()).append().append(entry.getValue()).append()} } .getForObject(urlBuiler.toString()cls)} }
post 请求直接调用post方法,传递url、params、返回的类型(可以自定义)
返回的类型,示例如下:
HttpResult { Integer String BgpValuseDto }