1 public class RnrFpRestTemplateUtils { 2 3 private static final Logger LOGGER = LoggerFactory.getLogger(RnrFpRestTemplateUtils.class); 4 5 6 /** 7 * Description: post请求Response泛型 8 * <br /> 9 * CreateDate 2021-11-01 16:53:25 10 * 11 * @author yuyi 12 **/ 13 public static <T> Response<T> postForResponse(RestTemplate restTemplate, String url, Object request, 14 Class<T> cls, Object... urlVariables) { 15 long startTime = System.currentTimeMillis(); 16 LOGGER.info("postForResponse url: {},request: {}", url, 17 JSON.toJSONString(request, new FpLogOutputJsonSerializeFilter())); 18 ResponseEntity<String> entity = 19 restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(request), String.class, urlVariables); 20 Response<T> result = JSON.parseObject(entity.getBody(), new TypeReference<Response<T>>(cls) { 21 }.getType()); 22 LOGGER.info("postForResponse url: {},response: {},cost: {} ms", url, 23 JSON.toJSONString(result, new FpLogOutputJsonSerializeFilter()), 24 System.currentTimeMillis() - startTime); 25 return result; 26 } 27 28 /** 29 * Description: post请求Response泛型 30 * <br /> 31 * CreateDate 2021-11-01 16:53:25 32 * 33 * @author yuyi 34 **/ 35 public static Response postForResponse(RestTemplate restTemplate, String url, Object request, 36 Object... urlVariables) { 37 long startTime = System.currentTimeMillis(); 38 LOGGER.info("postForResponse url: {},request: {}", url, 39 JSON.toJSONString(request, new FpLogOutputJsonSerializeFilter())); 40 ResponseEntity<String> entity = 41 restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(request), String.class, urlVariables); 42 Response result = JSON.parseObject(entity.getBody(), Response.class); 43 LOGGER.info("postForResponse url: {},response: {},cost: {} ms", url, 44 JSON.toJSONString(result, new FpLogOutputJsonSerializeFilter()), 45 System.currentTimeMillis() - startTime); 46 return result; 47 } 48 49 /** 50 * Description: post请求Response泛型 51 * <br /> 52 * CreateDate 2021-11-01 16:53:25 53 * 54 * @author yuyi 55 **/ 56 public static Response postOpenApiForResponse(RestTemplate restTemplate, String url, HttpEntity request, 57 Object... urlVariables) { 58 long startTime = System.currentTimeMillis(); 59 LOGGER.info("postOpenApiForResponse url: {},request: {}", url, 60 JSON.toJSONString(request, new FpLogOutputJsonSerializeFilter())); 61 ResponseEntity<String> entity = 62 restTemplate.exchange(url, HttpMethod.POST, request, String.class, urlVariables); 63 Response result = JSON.parseObject(entity.getBody(), Response.class); 64 LOGGER.info("postOpenApiForResponse url: {},response: {},cost: {} ms", url, 65 JSON.toJSONString(result, new FpLogOutputJsonSerializeFilter()), 66 System.currentTimeMillis() - startTime); 67 return result; 68 } 69 70 /** 71 * Description: post请求Response<PageResult>泛型 72 * <br /> 73 * CreateDate 2021-11-01 16:53:25 74 * 75 * @author yuyi 76 **/ 77 public static <T> Response<PageResult<T>> postForResponsePageResult(RestTemplate restTemplate, String url, 78 Object request, 79 Class<T> cls, Object... urlVariables) { 80 long startTime = System.currentTimeMillis(); 81 LOGGER.info("postForResponsePageResult url: {},request: {}", url, 82 JSON.toJSONString(request, new FpLogOutputJsonSerializeFilter())); 83 ResponseEntity<String> entity = 84 restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(request), String.class, urlVariables); 85 Response<PageResult<T>> result = JSON.parseObject(entity.getBody(), 86 new TypeReference<Response<PageResult<T>>>(cls) { 87 }.getType()); 88 LOGGER.info("postForResponsePageResult url: {},response: {},cost: {} ms", url, 89 JSON.toJSONString(result, new FpLogOutputJsonSerializeFilter()), 90 System.currentTimeMillis() - startTime); 91 return result; 92 } 93 94 /** 95 * Description: post请求Response<List>泛型 96 * <br /> 97 * CreateDate 2021-11-01 16:53:25 98 * 99 * @author yuyi 100 **/ 101 public static <T> Response<List<T>> postForResponseList(RestTemplate restTemplate, String url, Object request, 102 Class<T> cls, Object... urlVariables) { 103 long startTime = System.currentTimeMillis(); 104 LOGGER.info("postForResponseList url: {},request: {}", url, 105 JSON.toJSONString(request, new FpLogOutputJsonSerializeFilter())); 106 ResponseEntity<String> entity = 107 restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(request), String.class, urlVariables); 108 Response<List<T>> result = JSON.parseObject(entity.getBody(), new TypeReference<Response<List<T>>>(cls) { 109 }.getType()); 110 LOGGER.info("postForResponseList url: {},response: {},cost: {} ms", url, 111 JSON.toJSONString(result, new FpLogOutputJsonSerializeFilter()), 112 System.currentTimeMillis() - startTime); 113 return result; 114 } 115 116 /** 117 * Description: post请求泛型 118 * <br /> 119 * CreateDate 2021-11-01 16:53:25 120 * 121 * @author yuyi 122 **/ 123 public static <T> T postForObject(RestTemplate restTemplate, String url, Object request, 124 Class<T> cls, Object... urlVariables) { 125 long startTime = System.currentTimeMillis(); 126 LOGGER.info("postForObject url: {},request: {}", url, 127 JSON.toJSONString(request, new FpLogOutputJsonSerializeFilter())); 128 ResponseEntity<String> entity = 129 restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(request), String.class, urlVariables); 130 LOGGER.info("postForObject url: {},response: {},cost: {} ms", url, 131 JSON.toJSONString(entity.getBody(), new FpLogOutputJsonSerializeFilter()), 132 System.currentTimeMillis() - startTime); 133 return JSON.parseObject(entity.getBody(), cls); 134 } 135 136 /** 137 * Description: post请求泛型 138 * <br /> 139 * CreateDate 2021-11-01 16:53:25 140 * 141 * @author yuyi 142 **/ 143 public static <T> T postOpenApiForObject(RestTemplate restTemplate, String url, HttpEntity request, 144 Class<T> cls, Object... urlVariables) { 145 long startTime = System.currentTimeMillis(); 146 LOGGER.info("postOpenApiForObject url: {},request: {}", url, 147 JSON.toJSONString(request, new FpLogOutputJsonSerializeFilter())); 148 ResponseEntity<String> entity = 149 restTemplate.exchange(url, HttpMethod.POST, request, String.class, urlVariables); 150 LOGGER.info("postOpenApiForObject url: {},response: {},cost: {} ms", url, 151 JSON.toJSONString(entity.getBody(), new FpLogOutputJsonSerializeFilter()), 152 System.currentTimeMillis() - startTime); 153 return JSON.parseObject(entity.getBody(), cls); 154 } 155 156 /** 157 * Description: post请求泛型 158 * <br /> 159 * CreateDate 2021-11-01 16:53:25 160 * 161 * @author yuyi 162 **/ 163 public static <T> T postForObject(RestTemplate restTemplate, String url, HttpEntity<?> requestEntity, 164 Class<T> cls, Object... urlVariables) { 165 long startTime = System.currentTimeMillis(); 166 LOGGER.info("postForObject url: {},request: {}", url, 167 JSON.toJSONString(requestEntity, new FpLogOutputJsonSerializeFilter())); 168 ResponseEntity<String> entity = 169 restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class, urlVariables); 170 T result = JSON.parseObject(entity.getBody(), cls); 171 LOGGER.info("postForObject url: {},response: {},cost: {} ms", url, 172 JSON.toJSONString(result, new FpLogOutputJsonSerializeFilter()), 173 System.currentTimeMillis() - startTime); 174 return result; 175 } 176 177 /** 178 * Description: get 请求响应泛型对象 179 * <br /> 180 * CreateDate 2022-05-13 18:41:15 181 * 182 * @author yuyi 183 **/ 184 public static <T> T getForObject(RestTemplate restTemplate, String url, Class<T> cls) { 185 long startTime = System.currentTimeMillis(); 186 LOGGER.info("getForObject url: {}", url); 187 ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class); 188 T result = JSON.parseObject(entity.getBody(), cls); 189 LOGGER.info("getForObject url: {},response: {},cost: {} ms", url, 190 JSON.toJSONString(result, new FpLogOutputJsonSerializeFilter()), 191 System.currentTimeMillis() - startTime); 192 return result; 193 } 194 195 /** 196 * Description: post请求Response泛型 197 * <br /> 198 * CreateDate 2021-11-01 16:53:25 199 * 200 * @author yuyi 201 **/ 202 public static <T> Response<T> getForResponse(RestTemplate restTemplate, String url, Class<T> cls) { 203 long startTime = System.currentTimeMillis(); 204 LOGGER.info("getForResponse url: {}", url); 205 ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class); 206 Response<T> result = JSON.parseObject(entity.getBody(), new TypeReference<Response<T>>(cls) { 207 }.getType()); 208 LOGGER.info("getForResponse url: {},response: {},cost: {} ms", url, 209 JSON.toJSONString(result, new FpLogOutputJsonSerializeFilter()), 210 System.currentTimeMillis() - startTime); 211 return result; 212 } 213 }View Code
标签:调用,封装,url,request,resttemplate,entity,JSON,result,new From: https://www.cnblogs.com/pengmt/p/17420377.html