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 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南