| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- package com.ydtech.modules.order.components;
- import com.alibaba.fastjson.JSON;
- import com.ydtech.constants.InsuranceEnum;
- import com.ydtech.exception.SystemException;
- import com.ydtech.modules.order.utils.InsuranceLog;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.http.HttpEntity;
- import org.springframework.http.HttpHeaders;
- import org.springframework.http.MediaType;
- import org.springframework.http.ResponseEntity;
- import org.springframework.lang.Nullable;
- import org.springframework.stereotype.Component;
- import org.springframework.web.client.RestTemplate;
- @Slf4j
- @Component
- public class InsCrawlerOrderComponents {
- private final RestTemplate restTemplate;
- public InsCrawlerOrderComponents(RestTemplate restTemplate) {
- this.restTemplate = restTemplate;
- }
- public <T, B> B post(String url, String logId, String message, @Nullable T requestBody, Class<B> responseType, HttpHeaders httpHeaders) {
- httpHeaders.setContentType(MediaType.APPLICATION_JSON);
- HttpEntity<T> yaQuoteInfoVoHttpEntity = new HttpEntity<>(requestBody, httpHeaders);
- String request = JSON.toJSONString(requestBody);
- InsuranceLog.infoLog(logId, "\n\t---------> {} \n\t---------> URL: {} \n\t---------> 请求参数: {}", message, url, request);
- ResponseEntity<B> stringResponseEntity = restTemplate.postForEntity(url, yaQuoteInfoVoHttpEntity, responseType);
- B body = stringResponseEntity.getBody();
- if (body == null) {
- throw new SystemException(message + "接口请求失败");
- }
- String response = JSON.toJSONString(body);
- InsuranceLog.infoLog(logId, "\n\t---------> {} \n\t---------> 响应参数:{}", message, response);
- return body;
- }
- public <T, B> B request(String url, String companyId, String apiType, T requestBody, Class<B> responseType) {
- // 获取保险公司枚举
- String substring = companyId.substring(0, 4);
- InsuranceEnum insuranceEnum = Enum.valueOf(InsuranceEnum.class, substring);
- return this.request(url, insuranceEnum, apiType, requestBody, responseType, new HttpHeaders());
- }
- public <T, B> B request(String url, InsuranceEnum insuranceEnum, String apiType, T requestBody, Class<B> responseType) {
- return this.request(url, insuranceEnum, apiType, requestBody, responseType, new HttpHeaders());
- }
- public <T, B> B request(String url, InsuranceEnum insuranceEnum, String apiType, T requestBody, Class<B> responseType, HttpHeaders httpHeaders) {
- String message = insuranceEnum.getName() + apiType;
- return this.post(url, insuranceEnum.getPinyin(), message, requestBody, responseType, httpHeaders);
- }
- }
|