Java restTemplete 方式请求第三方接口(GET、POST、PATCH、PUT、DELETE)及上传文件

引入Jar包

		<!--spring restTemplate-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>

HttpRequestUtils

import com.alibaba.fastjson.JSONObject;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

public class MyHttpRequest {

	private static RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());

	private static String token = "";

	/**
	 *  获取完整的url
	 */
	private static String getUrl (String url) {
		return "http://192.168.**.**:****/api/v1" + url;
	}

	/**
	 * 登录 获取token 需要加上定时任务
	 */
	public static void login() {
		ResponseEntity<String> result = sendPost("/servicesmngt/login", "{\"user\":\"***\",\"password\":\"******\"}");
		JSONObject reJsonObject = JSONObject.parseObject(result.getBody());
		token = reJsonObject.getString("token");
		System.out.println(new Date() + "	登录");
	}
	/**
	 * 上传文件
	 */
	public static ResponseEntity<String> uploadFiles(String url, MultipartFile file) {

		// 配置请求头
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.MULTIPART_FORM_DATA);
		headers.set("token", token);
		
		// 处理参数
		MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
		parts.add("file",file.getResource());
		//这里还可以加上别的参数 类似这句 parts.add("file",file.getResource());
		
		HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(parts, headers);

		// 发起请求
		ResponseEntity<String> res = restTemplate.exchange(getUrl(url), HttpMethod.POST, httpEntity, String.class);
		return res;
	}
	/**
	 * Get
	 */
	public static ResponseEntity<String> sendGet(String url, String param) {

		HttpHeaders headers = new HttpHeaders();
		headers.set("Content-Type","application/json");
		headers.set("token", token);

		HttpEntity httpEntity = new HttpEntity(param, headers);

		ResponseEntity<String> res = restTemplate.exchange(getUrl(url), HttpMethod.GET, httpEntity, String.class);

		return res;
	}
	/**
	 * Post
	 */
	public static ResponseEntity<String> sendPost(String url, String param) {

		HttpHeaders headers = new HttpHeaders();
		headers.set("Content-Type","application/json");
		headers.set("token", token);

		HttpEntity httpEntity = new HttpEntity(param, headers);

		ResponseEntity<String> res = restTemplate.exchange(getUrl(url), HttpMethod.POST, httpEntity, String.class);

		return res;
	}
	/**
	 * Patch
	 */
	public static ResponseEntity<String> sendPatch(String url, String param) {
		HttpHeaders headers = new HttpHeaders();
		headers.set("Content-Type","application/json");
		headers.set("token", token);

		HttpEntity httpEntity = new HttpEntity(param, headers);

		ResponseEntity<String> res = restTemplate.exchange(getUrl(url), HttpMethod.PATCH, httpEntity, String.class);

		return res;
	}
	/**
	 * Delete
	 */
	public static ResponseEntity<String> sendDelete(String url, String param) {
		HttpHeaders headers = new HttpHeaders();
		headers.set("Content-Type","application/json");
		headers.set("token", token);

		HttpEntity httpEntity = new HttpEntity(param, headers);

		ResponseEntity<String> res = restTemplate.exchange(getUrl(url), HttpMethod.DELETE, httpEntity, String.class);

		return res;
	}
	
	/**
	 * Put
	 */
	public static ResponseEntity<String> sendPut(String url, String param) {
		HttpHeaders headers = new HttpHeaders();
		headers.set("token", token);
		headers.setContentType(MediaType.APPLICATION_JSON);
		HttpEntity httpEntity = new HttpEntity(param, headers);

		ResponseEntity<String> res = restTemplate.exchange(getUrl(url), HttpMethod.PUT, httpEntity, String.class);

		return res;
	}

// end
}

另外:
这里的param都是json格式

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值