package com.template.model.weixin; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; /** * @Author: codingliang * @Description: http工具 * @Date: 2021-06-07 10:52 * @Version: V1.0 **/ public class MyRestTempleteUtil { /** * 微信支付域名【中国国内可用域名】 */ private static final String WECHATPAYV3HOST = "https://api.mch.weixin.qq.com"; /** * 请求微信支付V3接口 * @param urlSuffix 请求接口地址,需要以 ”/“ 开头 * @param httpMethod 请求方法 * @param authStr auth字符串 * @param body 请求参数 * @return */ public ResponseEntity exchangeForWechatV3Pay(String urlSuffix, HttpMethod httpMethod, String authStr, String body) { // 构建请求头 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); headers.add("Authorization", authStr); headers.add("Wechatpay-Serial", "61A33563F6158C1B921A27A23A4E94E963DD53DD"); // 61A** 平台证书 headers.add("User-Agent", "WeChatPay-HttpClient/null (Windows 10/10.0) Java/1.8.0_171"); headers.add("Accept", "application/json"); headers.add("Content-Type", "application/json"); // 构建请求参数 HttpEntity entity = new HttpEntity<>(body, headers); RestTemplate restTemplate = new RestTemplate(); ResponseEntity responseEntity; try { // 发送请求 responseEntity = restTemplate.exchange(WECHATPAYV3HOST.concat(urlSuffix), httpMethod, entity, String.class); System.out.println("responseEntity = " + responseEntity); } catch (Exception e) { e.printStackTrace(); throw new RRException(BizCodeEnume.GET_PAY_PARAM_ERROR); } // 返回响应结果 return responseEntity; } }