MyRestTempleteUtil.java 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package com.template.model.weixin;
  2. import org.springframework.http.HttpEntity;
  3. import org.springframework.http.HttpHeaders;
  4. import org.springframework.http.HttpMethod;
  5. import org.springframework.http.MediaType;
  6. import org.springframework.http.ResponseEntity;
  7. import org.springframework.web.client.RestTemplate;
  8. /**
  9. * @Author: codingliang
  10. * @Description: http工具
  11. * @Date: 2021-06-07 10:52
  12. * @Version: V1.0
  13. **/
  14. public class MyRestTempleteUtil {
  15. /**
  16. * 微信支付域名【中国国内可用域名】
  17. */
  18. private static final String WECHATPAYV3HOST = "https://api.mch.weixin.qq.com";
  19. /**
  20. * 请求微信支付V3接口
  21. * @param urlSuffix 请求接口地址,需要以 ”/“ 开头
  22. * @param httpMethod 请求方法
  23. * @param authStr auth字符串
  24. * @param body 请求参数
  25. * @return
  26. */
  27. public ResponseEntity<String> exchangeForWechatV3Pay(String urlSuffix, HttpMethod httpMethod, String authStr, String body) {
  28. // 构建请求头
  29. HttpHeaders headers = new HttpHeaders();
  30. headers.setContentType(MediaType.APPLICATION_JSON);
  31. headers.add("Authorization", authStr);
  32. headers.add("Wechatpay-Serial", "61A33563F6158C1B921A27A23A4E94E963DD53DD"); // 61A** 平台证书
  33. headers.add("User-Agent", "WeChatPay-HttpClient/null (Windows 10/10.0) Java/1.8.0_171");
  34. headers.add("Accept", "application/json");
  35. headers.add("Content-Type", "application/json");
  36. // 构建请求参数
  37. HttpEntity<String> entity = new HttpEntity<>(body, headers);
  38. RestTemplate restTemplate = new RestTemplate();
  39. ResponseEntity<String> responseEntity;
  40. try {
  41. // 发送请求
  42. responseEntity = restTemplate.exchange(WECHATPAYV3HOST.concat(urlSuffix), httpMethod, entity, String.class);
  43. System.out.println("responseEntity = " + responseEntity);
  44. } catch (Exception e) {
  45. e.printStackTrace();
  46. throw new RRException(BizCodeEnume.GET_PAY_PARAM_ERROR);
  47. }
  48. // 返回响应结果
  49. return responseEntity;
  50. }
  51. }