详解使用Spring的restTemplete进行Http请求
程序员文章站
2023-12-21 15:32:04
最近学习spring框架,本文介绍了使用spring的resttemplete进行http请求,留个笔记
/*
* rest templete
*/
pub...
最近学习spring框架,本文介绍了使用spring的resttemplete进行http请求,留个笔记
/* * rest templete */ public class resttemplatetest { //private static string url = "http://10.2.1.46:8080"; private static string url = "http://127.0.0.1:8080/test"; private static string autodeploy = "/api/ddd/manages/yyy/1111"; public static void main(string[] args) throws unsupportedencodingexception { jsonobject token1 = logintest.login(url); jsonobject token = new jsonobject(); token.put("token", token1.getstring("token")); token.put("username", "chenchen.ming@56qq.com"); string name = "明辰晨"; token.put("name", urlencoder.encode(name, "utf-8")); string jsonbody = dmsautodeploytest.dmsautodeploydata(); autodeploy(jsonbody,token.tojsonstring()); } /* * 主要的post方法 */ public static void autodeploy(string json,string token){ string autodeployurl = url + autodeploy; resttemplate resttemplate = new resttemplate(); httpheaders headers = new httpheaders(); //一定要设置好contenttype为utf8否则会乱码 mediatype type = mediatype.parsemediatype("application/json; charset=utf-8"); headers.setcontenttype(type); headers.add("accept", mediatype.application_json.tostring()); headers.add("authentication", token);//设置自定义session header httpentity<string> formentity = new httpentity<string>(json, headers); map<string, object> parametermap = new hashmap<>(); //entity = header,urivariables = parameters; resttemplate.postforobject(autodeployurl, formentity, string.class, parametermap); } /* * 测试 */ public static void test4(){ jsonobject response = null; map<string,object> param = new hashmap<>(); param.put("ming", "chyen"); string json = "haha"; try { response = restutil.post("http://127.0.0.1:8080/cloud-master/api/release/manages/detail", jsonobject.class,null,null,json); } catch (exception e) { e.printstacktrace(); } system.out.println(response); } /** * 测试 */ public static void test(){ resttemplate resttemplate = new resttemplate(); map<string, object> parametermap = new hashmap<>(); resttemplate.getforobject("url", string.class,parametermap); //factory.createrequest(uri, httpmethod) } }
util
/** * 使用spring的resttemplate进行http请求 * @author mingchenchen * */ public class restutil { private static resttemplate resttemplate = new resttemplate(); /** * get方法 * * @param url:地址 * @param returnclassname:返回对象类型,如:string.class * @param parameters:parameter参数 * @return */ public static <t> t get(string url, class<t> returnclassname, map<string, object> parameters){ if (parameters == null) { return resttemplate.getforobject(url, returnclassname); } return resttemplate.getforobject(url, returnclassname, parameters); } /** * post请求,包含了路径,返回类型,header,parameter * * @param url:地址 * @param returnclassname:返回对象类型,如:string.class * @param inputheader * @param inputparameter * @param jsonbody * @return */ public static <t> t post(string url,class<t> returnclassname,map<string,object> inputheader,map<string,object> inputparameter,string jsonbody){ //请求header httpheaders httpheaders = new httpheaders(); //拼接header if (inputheader != null) { set<string> keys = inputheader.keyset(); for (iterator<string> i = keys.iterator(); i.hasnext();) { string key = (string) i.next(); httpheaders.add(key, inputheader.get(key).tostring()); } } //设置请求的类型及编码 mediatype type = mediatype.parsemediatype("application/json; charset=utf-8"); httpheaders.setcontenttype(type); httpheaders.add("accept", "application/json"); list<mediatype> acceptablemediatypes = new arraylist<>(); acceptablemediatypes.add(mediatype.all); httpheaders.setaccept(acceptablemediatypes); httpentity<string> formentity = new httpentity<string>(jsonbody, httpheaders); if (inputparameter==null) { return resttemplate.postforobject(url, formentity, returnclassname); } return resttemplate.postforobject(url, formentity, returnclassname, inputparameter); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
spring cloud 之 Feign 使用HTTP请求远程服务的实现方法
-
spring cloud 之 Feign 使用HTTP请求远程服务的实现方法
-
详解Spring cloud使用Ribbon进行Restful请求
-
详解Spring Boot配置使用Logback进行日志记录的实战
-
spring boot中使用http请求的示例代码
-
详解Spring cloud使用Ribbon进行Restful请求
-
详解Spring Boot配置使用Logback进行日志记录的实战
-
nodejs 使用http进行post或get请求的实例(携带cookie)
-
详解Idea中HTTP Client请求测试工具的使用
-
在 Angular6 中使用 HTTP 请求服务端数据的步骤详解