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"));
            }

}


}
相关推荐
_oP_i16 分钟前
Pinpoint 是一个开源的分布式追踪系统
java·分布式·开源
mmsx19 分钟前
android sqlite 数据库简单封装示例(java)
android·java·数据库
武子康44 分钟前
大数据-258 离线数仓 - Griffin架构 配置安装 Livy 架构设计 解压配置 Hadoop Hive
java·大数据·数据仓库·hive·hadoop·架构
豪宇刘2 小时前
MyBatis的面试题以及详细解答二
java·servlet·tomcat
秋恬意2 小时前
Mybatis能执行一对一、一对多的关联查询吗?都有哪些实现方式,以及它们之间的区别
java·数据库·mybatis
FF在路上3 小时前
Knife4j调试实体类传参扁平化模式修改:default-flat-param-object: true
java·开发语言
真的很上进3 小时前
如何借助 Babel+TS+ESLint 构建现代 JS 工程环境?
java·前端·javascript·css·react.js·vue·html
众拾达人3 小时前
Android自动化测试实战 Java篇 主流工具 框架 脚本
android·java·开发语言
皓木.3 小时前
Mybatis-Plus
java·开发语言
不良人天码星3 小时前
lombok插件不生效
java·开发语言·intellij-idea