package com.congge.utils;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.Iterator;
import java.util.Map;
@Component
public class OkHttpUtils {
private static final Logger logger = LoggerFactory.getLogger(OkHttpUtils.class);
@Autowired
private OkHttpClient okHttpClient;
/**
* @param url 请求的url
* @param paramMap 请求的参数,在浏览器?后面的数据,没有可以传null
* @return
*/
public String get(String url, Map<String, Object> paramMap) {
paramCheck(url, paramMap);
String resBody = "";
StringBuffer sb = new StringBuffer(url);
boolean firstFlag = true;
Iterator iterator = paramMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry<String, String>) iterator.next();
if (firstFlag) {
sb.append("?" + entry.getKey() + "=" + entry.getValue());
firstFlag = false;
} else {
sb.append("&" + entry.getKey() + "=" + entry.getValue());
}
}
Request request = new Request.Builder()
.url(sb.toString())
.build();
Response response = null;
try {
response = okHttpClient.newCall(request).execute();
int status = response.code();
if(200 !=status){
throw new RuntimeException("调用失败");
}
if (response.isSuccessful()) {
return response.body().string();
}
} catch (Exception e) {
logger.error("okhttp3 put error >> ex = {}", e.getMessage());
} finally {
if (response != null) {
response.close();
}
}
return resBody;
}
private void paramCheck(String url, Map<String, Object> paramMap) {
if(StringUtils.isEmpty(url)){
throw new RuntimeException("url不能为空");
}
if(null == paramMap || paramMap.isEmpty()){
throw new RuntimeException("参数不能为空");
}
}
/**
* post请求
* @param url
* @param json
* @return
* @throws Exception
*/
public String post(String url, String json) throws Exception {
RequestBody body = RequestBody.create(json, MediaType.get("application/json"));
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = okHttpClient.newCall(request).execute();
return response.body().string();
}
/**
* post
*
* @param url 请求的url
* @param params post form 提交的参数
* @return
*/
public String post(String url, Map<String, Object> params) {
paramCheck(url, params);
String responseBody = "";
FormBody.Builder builder = new FormBody.Builder();
//添加参数
if (params != null && params.keySet().size() > 0) {
for (String key : params.keySet()) {
builder.add(key, params.get(key).toString());
}
}
Request request = new Request.Builder()
.url(url)
.post(builder.build())
.addHeader("Content-Type", "application/json")
.build();
Response response = null;
try {
response = okHttpClient.newCall(request).execute();
int status = response.code();
if(200 !=status){
throw new RuntimeException("调用失败");
}
if (response.isSuccessful()) {
return response.body().string();
}
} catch (Exception e) {
logger.error("okhttp3 post error >> ex = {}", e.getMessage());
} finally {
if (response != null) {
response.close();
}
}
return responseBody;
}
/**
* @param url 请求的url
* @param paramMap 请求的参数,在浏览器?后面的数据,没有可以传null
* @return
*/
public String getForHeader(String url, Map<String, Object> paramMap) {
paramCheck(url,paramMap);
String responseBody = "";
StringBuffer sb = new StringBuffer(url);
boolean firstFlag = true;
Iterator iterator = paramMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry<String, String>) iterator.next();
if (firstFlag) {
sb.append("?" + entry.getKey() + "=" + entry.getValue());
firstFlag = false;
} else {
sb.append("&" + entry.getKey() + "=" + entry.getValue());
}
}
Request request = new Request.Builder()
.addHeader("key", "value")
.url(sb.toString())
.build();
Response response = null;
try {
response = okHttpClient.newCall(request).execute();
int status = response.code();
if(200 !=status){
throw new RuntimeException("调用失败");
}
if (response.isSuccessful()) {
return response.body().string();
}
} catch (Exception e) {
logger.error("okhttp3 put error >> ex = {}", e.getMessage());
} finally {
if (response != null) {
response.close();
}
}
return responseBody;
}
/**
* Post请求发送JSON数据....{"name":"zhangsan","pwd":"123456"}
* 参数一:请求Url
* 参数二:请求的JSON
* 参数三:请求回调
*/
public String postJsonParams(String url, String jsonParams) {
String responseBody = "";
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonParams);
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
Response response = null;
try {
response = okHttpClient.newCall(request).execute();
int status = response.code();
if(200 !=status){
throw new RuntimeException("调用失败");
}
if (response.isSuccessful()) {
return response.body().string();
}
} catch (Exception e) {
logger.error("okhttp3 post error >> ex = {}", e.getMessage());
} finally {
if (response != null) {
response.close();
}
}
return responseBody;
}
/**
* Post请求发送xml数据....
* 参数一:请求Url
* 参数二:请求的xmlString
* 参数三:请求回调
*/
public String postXmlParams(String url, String xml) {
String responseBody = "";
RequestBody requestBody = RequestBody.create(MediaType.parse("application/xml; charset=utf-8"), xml);
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
Response response = null;
try {
response = okHttpClient.newCall(request).execute();
int status = response.code();
if(200 !=status){
throw new RuntimeException("调用失败");
}
if (response.isSuccessful()) {
return respo
没有合适的资源?快使用搜索试试~ 我知道了~
spring aop实现接口参数变更前后对比和日志记录
共95个文件
java:38个
class:37个
xml:15个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 200 浏览量
2024-05-12
19:06:04
上传
评论 1
收藏 91KB ZIP 举报
温馨提示
spring aop实现接口参数变更前后对比和日志记录完整代码,拿到项目代码,只需要做数据库连接的修改即可运行起来使用,代码案例详细,真是可靠,代码原文地址:https://blog.csdn.net/zhangcongyi420/article/details/138748857?spm=1001.2014.3001.5501
资源推荐
资源详情
资源评论
收起资源包目录
116.zip (95个子文件)
116
pom.xml 3KB
src
test
java
main
resources
mapper
TbUserMapper.xml 1KB
OperateLogMapper.xml 547B
SqlLogMapper.xml 534B
FieldChangeLogMapper.xml 458B
application.yml 437B
java
com
congge
controller
HttpClientController.java 2KB
OkHttpController.java 1KB
TbUserController.java 2KB
RestTemController.java 6KB
utils
SpringContextUtil.java 2KB
OkHttpUtils.java 8KB
ReflectionUtils.java 5KB
HttpClientUtil.java 5KB
dao
TbUserMapper.java 266B
FieldChangeLogMapper.java 167B
BusInfoModifyHisDao.java 70B
SqlLogMapper.java 151B
OperateLogMapper.java 148B
service
BusInfoModifyHisService.java 78B
TbUserService.java 213B
CommonUserService.java 414B
impl
BusInfoModifyHisServiceImpl.java 230B
TbUserServiceImpl.java 860B
enums
HttpApp.java 479B
entity
DbUser.java 230B
SqlLogPo.java 368B
BusInfoModifyHis.java 526B
FieldChangeLog.java 264B
TbUser.java 552B
log
OperateType.java 402B
OperationLog.java 1KB
BusinessType.java 818B
DataName.java 226B
SqlLogAspect.java 7KB
TbUserHandleOperateLogAspect.java 7KB
Log.java 530B
OperateLog.java 320B
SqlLog.java 831B
TbUserLogAspect.java 3KB
MyInvocationHandler.java 455B
SqlLogFilter.java 305B
config
OkHttpConfig.java 323B
RestTemplateConfig.java 1KB
.idea
.name 9B
jarRepositories.xml 1KB
uiDesigner.xml 9KB
workspace.xml 10KB
misc.xml 526B
compiler.xml 861B
.gitignore 218B
encodings.xml 191B
target
classes
mapper
TbUserMapper.xml 1KB
OperateLogMapper.xml 547B
SqlLogMapper.xml 534B
FieldChangeLogMapper.xml 458B
application.yml 451B
com
congge
HttpApp.class 786B
controller
HttpClientController.class 2KB
TbUserController.class 3KB
OkHttpController.class 2KB
utils
HttpClientUtil.class 7KB
ReflectionUtils.class 5KB
SpringContextUtil.class 2KB
OkHttpUtils.class 8KB
dao
FieldChangeLogMapper.class 243B
BusInfoModifyHisDao.class 134B
OperateLogMapper.class 222B
TbUserMapper.class 460B
SqlLogMapper.class 221B
service
TbUserService.class 380B
CommonUserService.class 768B
BusInfoModifyHisService.class 146B
impl
TbUserServiceImpl.class 1KB
BusInfoModifyHisServiceImpl.class 487B
entity
FieldChangeLog.class 4KB
BusInfoModifyHis.class 647B
SqlLogPo.class 5KB
TbUser.class 3KB
DbUser.class 2KB
log
MyInvocationHandler.class 913B
SqlLogAspect.class 9KB
DataName.class 493B
TbUserHandleOperateLogAspect.class 5KB
SqlLog.class 645B
TbUserLogAspect.class 5KB
Log.class 672B
SqlLogFilter.class 529B
OperateType.class 1KB
OperationLog.class 858B
BusinessType.class 2KB
OperateLog.class 4KB
config
RestTemplateConfig.class 2KB
OkHttpConfig.class 591B
generated-sources
annotations
boot-http.iml 81B
共 95 条
- 1
资源评论
小码农叔叔
- 粉丝: 6w+
- 资源: 86
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功