RenbaoRequestComponents.java 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package com.ydtech.components;
  2. import com.alibaba.fastjson.JSON;
  3. import com.ydtech.aop.request.MethodLogAnnotation;
  4. import com.ydtech.exception.SystemException;
  5. import com.ydtech.modules.ins.model.request.Response;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.beans.factory.annotation.Value;
  9. import org.springframework.core.ParameterizedTypeReference;
  10. import org.springframework.http.HttpEntity;
  11. import org.springframework.http.HttpHeaders;
  12. import org.springframework.http.MediaType;
  13. import org.springframework.http.ResponseEntity;
  14. import org.springframework.lang.Nullable;
  15. import org.springframework.stereotype.Component;
  16. import org.springframework.web.client.RestTemplate;
  17. @Slf4j
  18. @Component
  19. public class RenbaoRequestComponents {
  20. @Autowired
  21. private RestTemplate restTemplate;
  22. @Value("${rb.quote.url}")
  23. private String rbQuoteUrl;
  24. @Value("${rb.audit.url}")
  25. private String rbAuditUrl;
  26. @Value("${rb.car.search.url}")
  27. private String rbCarSearchUrl;
  28. @Value("${rb.brand.search.url}")
  29. private String rbBrandSearchUrl;
  30. // @MethodLogAnnotation(title = "报价-请求python")
  31. public <T, B> B post(String url, String message, @Nullable T requestBody, Class<B> responseType) {
  32. HttpHeaders httpHeaders = new HttpHeaders();
  33. httpHeaders.setContentType(MediaType.APPLICATION_JSON);
  34. HttpEntity<T> yaQuoteInfoVoHttpEntity = new HttpEntity<>(requestBody, httpHeaders);
  35. String request = JSON.toJSONString(requestBody);
  36. log.info("---------> 请求参数:{}", request);
  37. ResponseEntity<B> stringResponseEntity = restTemplate.postForEntity(url, yaQuoteInfoVoHttpEntity, responseType);
  38. B body = stringResponseEntity.getBody();
  39. if (body == null) {
  40. throw new SystemException(message + "接口请求失败");
  41. }
  42. String response = JSON.toJSONString(body);
  43. log.info("---------> 请求参数:{}", response);
  44. return body;
  45. }
  46. public <T, B> B quote(T t, Class<B> responseType) {
  47. return post(rbQuoteUrl, "人保报价", t, responseType);
  48. }
  49. public <T, B> B audit(T t, Class<B> responseType) {
  50. return post(rbAuditUrl, "人保核保", t, responseType);
  51. }
  52. public <T, B> B licensePlateQuery(T t, Class<B> responseType) {
  53. return post(rbCarSearchUrl, "人保车牌号查询车辆信息", t, responseType);
  54. }
  55. public <T, B> B modelsQuery(T t, Class<B> responseType) {
  56. return post(rbBrandSearchUrl, "人保车辆查询", t, responseType);
  57. }
  58. }