1.工具类如下
@Component
@Slf4j
public class RestClientUtil<T> {
private final RestTemplate restTemplate;
public RestClientUtil() {
this.restTemplate = new RestTemplate();
}
public JSONObject uploadFile(String url, String fileUrl) throws IOException {
List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
messageConverters.add(new MappingJackson2HttpMessageConverter());
messageConverters.add(new FormHttpMessageConverter());
messageConverters.add(new FastJsonHttpMessageConverter());
restTemplate.setMessageConverters(messageConverters);
// 构建请求体
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
File file = new File(fileUrl);
Path filePath = Paths.get(file.getAbsolutePath());
byte[] fileBytes = Files.readAllBytes(filePath);
bodyBuilder.part("data", new ByteArrayResource(fileBytes) {
@Override
public String getFilename() {
return file.getName();
}
}, MediaType.MULTIPART_FORM_DATA);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, HttpEntity<?>>> requestEntity = new HttpEntity<>(bodyBuilder.build(), headers);
// 发送请求并处理响应
ResponseEntity<JSONObject> exchange = restTemplate.exchange(url, HttpMethod.POST, requestEntity, JSONObject.class);
if (exchange.getStatusCode() == HttpStatus.OK) {
return exchange.getBody();
}
return null;
}
public ResponseEntity<T> get(String url, Class<T> responseType) {
HttpHeaders headers = new HttpHeaders();
HttpEntity<String> requestEntity = new HttpEntity<>(headers);
return restTemplate.exchange(url, HttpMethod.GET, requestEntity, responseType);
}
public <T> T post(String url, MultiValueMap<String, String> requestBody, Class<T> responseType) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(requestBody, headers);
ResponseEntity<T> responseEntity = restTemplate.postForEntity(
url,
requestEntity,
responseType
);
return responseEntity.getBody();
}
public ResponseEntity<T> delete(String url, Class<T> responseType) {
HttpHeaders headers = new HttpHeaders();
HttpEntity<String> requestEntity = new HttpEntity<>(headers);
return restTemplate.exchange(url, HttpMethod.DELETE, requestEntity, responseType);
}
public ResponseEntity<T> put(String url, Object body, Class<T> responseType) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Object> requestEntity = new HttpEntity<>(body, headers);
return restTemplate.exchange(url, HttpMethod.PUT, requestEntity, responseType);
}
}
2.调用
@org.springframework.stereotype.Service
@Service(version = "1.0.0")
@Slf4j
public class OcrServiceImpl implements OcrService {
@Resource
private RedissonClient redissonClient;
@Autowired
private RestClientUtil<JSONObject> restClientUtil;
public BaseResponseVo<String> getLicenseInfo(OcrDTO ocrDTO) throws IOException {
JSONObject imgResult = restClientUtil.uploadFile(imgUrl + "?appkey=" + keyMapping.get(ocrDTO.getOcrType()), imagePath);
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
paramsMap.set("key", keyMapping.get(ocrDTO.getOcrType()));
paramsMap.set("imageId", imgResult.getString("data"));
JSONObject resultData = restClientUtil.post(url + typeMapping.get(ocrDTO.getOcrType()), paramsMap, JSONObject.class);
if (!"10000".equals(resultData.getString("code"))) {
return BaseResponseVo.error(imgResult.getString("code"), imgResult.getString("message"));
}
}
}
文章来源地址https://www.toymoban.com/news/detail-689038.html
文章来源:https://www.toymoban.com/news/detail-689038.html
到了这里,关于java里面封装https请求工具类的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!