這篇文章給大家介紹Spring中的Http請(qǐng)求如何使用restTemplete實(shí)現(xiàn),內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
網(wǎng)站建設(shè)公司,為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì)及定制網(wǎng)站建設(shè)服務(wù),專注于企業(yè)網(wǎng)站制作,高端網(wǎng)頁(yè)制作,對(duì)VR全景等多個(gè)行業(yè)擁有豐富的網(wǎng)站建設(shè)經(jīng)驗(yàn)的網(wǎng)站建設(shè)公司。專業(yè)網(wǎng)站設(shè)計(jì),網(wǎng)站優(yōu)化推廣哪家好,專業(yè)成都網(wǎng)站推廣優(yōu)化,H5建站,響應(yīng)式網(wǎng)站。
使用Spring的restTemplete進(jìn)行Http請(qǐng)求
/* * 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(); //一定要設(shè)置好ContentType為utf8否則會(huì)亂碼 MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8"); headers.setContentType(type); headers.add("Accept", MediaType.APPLICATION_JSON.toString()); headers.add("Authentication", token);//設(shè)置自定義session header HttpEntityformEntity = new HttpEntity (json, headers); Map parameterMap = new HashMap<>(); //entity = Header,uriVariables = Parameters; restTemplate.postForObject(autoDeployUrl, formEntity, String.class, parameterMap); } /* * 測(cè)試 */ 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); } /** * 測(cè)試 */ 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進(jìn)行http請(qǐng)求 * @author Mingchenchen * */ public class RestUtil { private static RestTemplate restTemplate = new RestTemplate(); /** * Get方法 * * @param url:地址 * @param returnClassName:返回對(duì)象類型,如:String.class * @param parameters:parameter參數(shù) * @return */ public staticT get(String url, Class returnClassName, Map parameters){ if (parameters == null) { return restTemplate.getForObject(url, returnClassName); } return restTemplate.getForObject(url, returnClassName, parameters); } /** * post請(qǐng)求,包含了路徑,返回類型,Header,Parameter * * @param url:地址 * @param returnClassName:返回對(duì)象類型,如:String.class * @param inputHeader * @param inputParameter * @param jsonBody * @return */ public static T post(String url,Class returnClassName,Map inputHeader,Map inputParameter,String jsonBody){ //請(qǐng)求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()); } } //設(shè)置請(qǐng)求的類型及編碼 MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8"); httpHeaders.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); } }
關(guān)于Spring中的Http請(qǐng)求如何使用restTemplete實(shí)現(xiàn)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。