private String generateScheme(GetWechatSchemeReq req, String accessToken) { String path = req.getPath(); String query = req.getQuery(); String url = "http://www.abc.com?abc=111"; WechatSchemeRequest wechatSchemeRequest = new WechatSchemeRequest(path, query, expireType, expireInterval); ResponseEntity<byte[]> responseEntity; try { responseEntity = restTemplate.exchange( url, HttpMethod.POST, createEntity(wechatSchemeRequest), new ParameterizedTypeReference<byte[]>() { } ); byte[] resultByte = responseEntity.getBody(); String resultStr = new String(resultByte, StandardCharsets.UTF_8); if (log.isDebugEnabled()) { log.debug("generateScheme url={},response={}", url, resultStr); } ObjectMapper object_mapper = new ObjectMapper(); JsonNode jsonNode = object_mapper.readValue(resultStr, JsonNode.class); JsonNode openlink = jsonNode.get("openlink"); if (openlink!=null) { return openlink.asText(); } log.error("generateScheme error url={},response={}", url, resultStr); throw new RuntimeException("获取scheme异常,error:" + resultStr); } catch (JsonMappingException e) { log.error("generateScheme error:{}", e); throw new RuntimeException(e); } catch (JsonProcessingException e) { log.error("generateScheme error:{}", e); throw new RuntimeException(e); } }
private static <Object> HttpEntity<String> createEntity(Object body) { HttpHeaders headers = new HttpHeaders(); MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8"); headers.setContentType(type); headers.add("Accept", MediaType.APPLICATION_JSON.toString()); String bodyJson = ""; if (body != null) { bodyJson = JsonUtil.toJSONString(body); } return new HttpEntity<>(bodyJson, headers); }