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());
}
}
}
springboot实现post请求的更多相关文章
- SpringBoot中异步请求和异步调用(看这一篇就够了)
原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/10661591.html,否则将追究法律责任!!! 一.SpringBoot中异步请求的使用 ...
- SpringBoot自定义错误信息,SpringBoot适配Ajax请求
SpringBoot自定义错误信息,SpringBoot自定义异常处理类, SpringBoot异常结果处理适配页面及Ajax请求, SpringBoot适配Ajax请求 ============== ...
- SpringBoot web获取请求数据【转】
SpringBoot web获取请求数据 一个网站最基本的功能就是匹配请求,获取请求数据,处理请求(业务处理),请求响应,我们今天来看SpringBoot中怎么获取请求数据. 文章包含的内容如下: 获 ...
- SpringBoot获取http请求参数的方法
SpringBoot获取http请求参数的方法 原文:https://www.cnblogs.com/zhanglijun/p/9403483.html 有七种Java后台获取前端传来参数的方法,稍微 ...
- SpringBoot AOP处理请求日志处理打印
SpringBoot AOP处理请求日志处理打印 @Slf4j @Aspect @Configuration public class RequestAopConfig { @Autowired pr ...
- SpringBoot之get请求404
后台:SpringBoot 前台:VUE 异常:调get接口,返回404 场景:get请求传参,后台返回n条数据,不传参则返回所有 原因:原请求url为"~/one/{param}" ...
- springboot跨域请求
首页 所有文章 资讯 Web 架构 基础技术 书籍 教程 Java小组 工具资源 SpringBoot | 番外:使用小技巧合集 2018/09/17 | 分类: 基础技术 | 0 条评论 | 标 ...
- 关于解决Springboot跨域请求的方法
前言 最近在项目中,由于前后分离,前台项目和后台项目部署的不在一台服务器,就产生了跨域的问题,特此记录下 正文 正常情况下,如果提示: 就可以判断是没有解决跨域的问题了. 在SSM中,我曾经这样解决过 ...
- springboot跨域请求设置
当它请求的一个资源是从一个与它本身提供的第一个资源的不同的域名时,一个资源会发起一个跨域HTTP请求(Cross-site HTTP request).比如说,域名A ( http://domaina ...
- 第一个SpringBoot插件-捕获请求并且支持重新发起
SpringBoot 插件入门 简介 公司用的是SpringBoot,api框架用的是swagger-ui,确实用的不错,但是在使用过程中发现一个问题,就是当前端正式调用的时候,如果参数一多的话模拟请 ...
随机推荐
- Apache和Nginx是什么?|Nginx和Reactor是什么?|网路IO的本质|阻塞队列|异步非阻塞IO
前言 那么这里博主先安利一些干货满满的专栏了! 首先是博主的高质量博客的汇总,这个专栏里面的博客,都是博主最最用心写的一部分,干货满满,希望对大家有帮助. 高质量干货博客汇总https://blog. ...
- 《ASP.NET Core 与 RESTful API 开发实战》-- (第7章)-- 读书笔记(下)
第 7 章 高级主题 7.4 HATEOAS 全称 Hypermedia AS The Engine Of Application State,即超媒体作为应用程序状态引擎.它作为 REST 统一界面 ...
- Flink-postgres-cdc实时同步报错:无法访问文件 "decoderbufs": 没有那个文件或目录
问题描述 Caused by: org.postgresql.util.PSQLException: 错误: 无法访问文件 "decoderbufs": 没有那个文件或目录 解决办 ...
- SpringBoot 2.6 和 JUnit 5 的测试用例注解和排序方式
JUnit5 的测试注解 在JUnit5中, 不再使用 @RunWith 注解, 改为使用 @ExtendWith(SpringExtension.class) @ExtendWith(SpringE ...
- 【Android】使用AIDL实现进程间传递对象案例
1 前言 在 Android--使用AIDL实现进程间通讯简单案例 中介绍了使用 AIDL 在进程间传递字符串,对于8种基本数据类型( byte.short.int.long.float.doub ...
- 通过performance_schema获取造成死锁的事务语句(转)
数据库日常维护中我们经常遇到死锁的问题,由于无法获取造成死锁的事务内执行过的语句,对我们死锁的分析造成很大的困难.但是在MySQL 5.7中我们可以利用performance_schema来获取这些语 ...
- spring boot+layui分页实战
项目用了layui,做了个简单的图书搜索页,分享出来. 喜欢的朋友给点个赞!!! 实现效果 开发步骤 1.前端页面和JS <!DOCTYPE html> <html xmlns=&q ...
- win32-UpdateLayeredWindow
创建半透明的窗口,以及在窗口上绘制不透明的文本 方法: 创建Bitmap具有PixelFormat32bppPARGB像素格式的GDI + 对象. 创建一个Graphics对象以绘制该Bitmap对象 ...
- RESTful API 介绍,设计
一:RESTful介绍 在互联网发展过程中,最开始是以html静态网页展示内容,url的表现形式一般为 http://www.example.com/getInfo.html:后来随着需求不断提高以及 ...
- [BUUCTF][Web][SUCTF 2019]EasySQL 1
这一题有点蛋疼,比较难顶 看了别人的write up 也很难get到解题思路,感觉必须要拿到源码进行审计才能解 大佬们猜后端是这么写的 select $_POST['query'] || flag f ...