RestTemplate發(fā)送get和post請求,下載文件的實例
下圖是我的所有測試接口,包含兩個表單提交接口和一個Rest接口:
我是用的Http請求工具是Spring自帶的RestTemplate。
請求的方法如下:
三個請求分別對應(yīng)三個接口,在此記錄下。
下載文件,獲取文件字節(jié)流:
RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); ResponseEntity<byte[]> entity = restTemplate.exchange("http://ip:port/test.doc", HttpMethod.GET,new HttpEntity<>(headers), byte[].class); byte[] body = entity.getBody();
multipart/form-data 文件上傳:
RestTemplate restTemplate = new RestTemplate(); String url = "http://127.0.0.1:8080/file/upload" MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>(); // 設(shè)置multi/form-data文件 multiValueMap.add("file", new FileSystemResource("D:/1.mp3")); multiValueMap.add("name", "測試材料"); // http請求 String response = restTemplate.postForObject(url, multiValueMap, String.class);
補充知識:restTemplate發(fā)送get與post請求 并且?guī)?shù)
我就廢話不多說了,大家還是直接看代碼吧~
@Test public void test() throws Exception{ String url = "http://localhost:8081/aa"; //headers HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.add("api-version", "1.0"); //body MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>(); requestBody.add("id", "1"); //HttpEntity HttpEntity<MultiValueMap> requestEntity = new HttpEntity<MultiValueMap>(requestBody, requestHeaders); //post ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, requestEntity, String.class); System.out.println(responseEntity.getBody()); ResponseEntity<String> responseEntity1 = restTemplate.exchange("http://172.26.186.206:8080/hive/list/schemas?appid=admin_test", HttpMethod.GET, requestEntity, String.class); System.out.println(responseEntity1.getBody()); }
restTemplate的注解如下:
@Component public class MyConfig { @Autowired RestTemplateBuilder builder; @Bean public RestTemplate restTemplate() { return builder.build(); } }
發(fā)送get請求
@Test public void testCheck() { String url = "http://172.26.186.206:8080/syncsql/process"; String timeStramp = String.valueOf(System.currentTimeMillis()); HttpHeaders headers = new HttpHeaders(); headers.add("appid", ""); headers.add("sign", sign(null, null,null)); headers.add("timestamp", timeStramp); JSONObject jsonObj = new JSONObject(); HttpEntity<String> formEntity = new HttpEntity<String>(null, headers); Map<String, Object> maps = new HashMap<String, Object>(); maps.put("sql", "select * from jingfen.d_user_city"); maps.put("type", 1); maps.put("account", "admin_test"); ResponseEntity<String> exchange = restTemplate.exchange(url + "?sql={sql}&type={type}&account={account}", HttpMethod.GET, formEntity, String.class, maps); String body = exchange.getBody(); LOGGER.info("{}", body); }
以上這篇RestTemplate發(fā)送get和post請求,下載文件的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
spring boot啟動出現(xiàn)Unable to start ServletWe
在使用SpringBoot時,啟動報錯可能源于多種原因,錯誤提示為缺少ServletWebServerFactory bean,初步分析可能是缺少spring-boot-starter-web依賴或@EnableAutoConfiguration注解,感興趣的可以了解一下2024-10-10Mybatis 中的一對一,一對多,多對多的配置原則示例代碼
這篇文章主要介紹了 Mybatis 中的一對一,一對多,多對多的配置原則示例代碼,需要的朋友可以參考下2017-03-03springcloud使用Hystrix進行微服務(wù)降級管理
這篇文章主要介紹了springcloud使用Hystrix進行微服務(wù)降級管理,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2019-04-04