java里面封装https请求工具类

1.工具类如下

java 复制代码
@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.调用

java 复制代码
@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"));
            }

}


}
相关推荐
芒果披萨11 分钟前
El表达式和JSTL
java·el
duration~1 小时前
Maven随笔
java·maven
zmgst1 小时前
canal1.1.7使用canal-adapter进行mysql同步数据
java·数据库·mysql
跃ZHD1 小时前
前后端分离,Jackson,Long精度丢失
java
blammmp1 小时前
Java:数据结构-枚举
java·开发语言·数据结构
暗黑起源喵2 小时前
设计模式-工厂设计模式
java·开发语言·设计模式
WaaTong2 小时前
Java反射
java·开发语言·反射
九圣残炎2 小时前
【从零开始的LeetCode-算法】1456. 定长子串中元音的最大数目
java·算法·leetcode
wclass-zhengge2 小时前
Netty篇(入门编程)
java·linux·服务器
Re.不晚3 小时前
Java入门15——抽象类
java·开发语言·学习·算法·intellij-idea