原csdn地址https://blog.csdn.net/wt1|

蜗牛使劲冲

园龄:7年6个月粉丝:3关注:10

springboot实现post请求

找了一堆,发现还是这个靠谱

package com.qishiyun.poplar.qlib.util;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.qishiyun.poplar.qlib.constant.ErrorCodeEnum;
import com.qishiyun.poplar.qlib.exception.QlibException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;


@Component
@Slf4j
@RefreshScope
public class OssUtil {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${newOss.baseUrl}")
    private String baseUrl;

    @Value("${newOss.stsUrl}")
    private String stsUrl;

    @Value("${newOss.appId}")
    private String appId;



    public String getOssTmpUrl(String ossUrl) {

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());

        JSONObject param = new JSONObject();
        param.put("ossUrl", ossUrl);
        param.put("appId",appId);
        param.put("expirationTime","1");
        HttpEntity<String> formEntity = new HttpEntity<String>(JSONUtil.toJsonStr(param), headers);


        String result = restTemplate.postForObject(baseUrl + stsUrl, formEntity, String.class);
        JSONObject resultJsonObject = JSONObject.parseObject(result);

        if (resultJsonObject.getInteger("code") == 200){
            JSONObject resultData = resultJsonObject.getJSONObject("data");
            String resultUrl = resultData.getString("ossSTSUrl");
            return resultUrl;
        }else {
            log.error("调用服务异常:{}",JSONUtil.toJsonStr(resultJsonObject.toJSONString()));
            throw new QlibException(ErrorCodeEnum.FAIL_GET_TMP_OSS_URL.code(), ErrorCodeEnum.FAIL_GET_TMP_OSS_URL.msg());
        }

    }

}

本文作者:蜗牛使劲冲

本文链接:https://www.cnblogs.com/warrenwt/p/18074536

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   蜗牛使劲冲  阅读(3)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起