详解使用Spring的restTemplete进行Http请求

网友投稿 294 2023-05-10


详解使用Spring的restTemplete进行Http请求

最近学习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";

prhttp://ivate 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 formEntity = new HttpEntity(json, headers);

Map parameterMap = new HashMap<>();

//entity = Header,uriVariables = Parameters;

restTemplate.postForObject(autoDeployUrl, formEntity, String.class, parameterMap);

}

/*

* 测试

*/

public static void test4(){

JSONObject response = null;

Map 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 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 get(String url, Class returnClassName, Map 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 post(String url,Class returnClassName,Map inputHeader,Map inputParameter,String jsonBody){

//请求Header

HttpHeaders httpHeaders = new HttpHeaders();

//拼接Header

if (inputHeader != null) {

Set keys = inputHeader.keySet();

for (Iterator 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");

httpHeahttp://ders.setContentType(type);

httpHeaders.add("Accept", "application/json");

List acceptableMediaTypes = new ArrayList<>();

acceptableMediaTypes.add(MediaType.ALL);

httpHeaders.setAccept(acceptableMediaTypes);

HttpEntity formEntity = new HttpEntity(jsonBody, httpHeaders);

if (inputParameter==null) {

return restTemplate.postForObject(url, formEntity, returnClassName);

}

return restTemplate.postForObject(url, formEntity, returnClassName, inputParameter);

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java concurrency之AtomicLongArray原子类_动力节点Java学院整理
下一篇:java实现多接口(java实现接口幂等性)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~