public static HttpResult AutoSend(this HttpRequest http) { HttpResult _result = http.Send(); StringBuilder _errMsg = new StringBuilder(); if (!_result.State) { _errMsg.AppendLine("Http-" + _result.Method + "错误:"); _errMsg.AppendLine("请求地址:" + _result.Url); _errMsg.Append("错误信息:" + _result.ErrMsg); Fn.Log.WriteInfo(_errMsg.ToStr()); throw Oops.Bah(_errMsg.ToString()); } else { if (_result.ContentType == HttpResponseContentType.Stream) { return _result; } else { if (_result.ResultJson == null) { _errMsg.AppendLine("Http-" + _result.Method + "错误:"); _errMsg.AppendLine("请求地址:" + _result.Url); _errMsg.AppendLine("错误信息:目标接口无返回数据!"); _errMsg.Append(_result.Result); throw Oops.Bah(_errMsg.ToString()); } if (_result.ResultJson["code"].ToInt() != 200) { if (_result.ResultJson["msg"].GetType() == typeof(JObject)) { JToken _msg = _result.ResultJson["msg"]; foreach (JProperty _jp in _msg.Cast<JProperty>()) { if (_jp.Value.GetType() == typeof(JArray)) { throw Oops.Oh(_jp.Value[0].ToStr()); } } } throw new Exception(_result.ResultJson["msg"].ToStr()); //_errMsg.AppendLine("Http-" + _result.Method + "错误:"); //_errMsg.AppendLine("请求地址:" + _result.Url); //_errMsg.AppendLine("错误信息:"+ _result.Result); //throw new Exception(_errMsg.ToStr()); } else { if (_result.ContentType == HttpResponseContentType.JSON) _result.ResultJson = _result.ResultJson["data"]; } return _result; } } }
时间: 2024-02-10 08:20:27 浏览: 140
这段代码看起来是一个扩展方法,将HttpRequest类型的实例对象扩展了一个名为AutoSend的方法。AutoSend方法似乎是用于自动发送HTTP请求并处理响应结果的。
具体来说,这个方法会先调用http对象的Send方法发送HTTP请求,然后根据响应结果的状态码、内容类型等信息进行处理。如果响应结果状态码不为200,就抛出异常;如果状态码为200,就在返回结果中提取"data"字段的值并返回。
需要注意的是,这段代码中有一些自定义的类型和方法,比如HttpResult、HttpResponseContentType、Oops.Bah等,这些类型和方法的具体实现需要结合上下文来理解。
相关问题
优化这个方法:private void saveFileBrowseRecord(LayoutElementParcelable file) { // 当前的目录 String currentPath = file.desc.substring(0, file.desc.lastIndexOf(File.separator)); String basePath = mSourceRootPath; if (mTransferType == U_FTP_TO_FAB_FTP) { basePath = mSourcePath; } XLog.tag(TAG).i("mCurrentPath:" + currentPath + " basePath:" + basePath); HybridFileParcelable hybridFileParcelable = new HybridFileParcelable(currentPath, basePath, file.desc, file.permissions, file.date, file.longSize, file.isDirectory, file.getMode(), ""); if (!TextUtils.isEmpty(file.title)) { hybridFileParcelable.setName(file.title); } Map<String, Object> map = new HashMap<>(); // 文件路径全名称 map.put("dirFullName", hybridFileParcelable.getRelativePath()); // 文件服务器id map.put("fileServerId", mStoreServerId); RequestBody body = HttpClient.getRequestBody(map); TransferApi api = HttpClient.api(TransferApi.class, false); if (api == null) { XLog.tag(TAG).i("save file browse record api is null"); return; } // recordType 记录类型:默认0-最近浏览,1-收藏 api.saveFileBrowseRecord(body, 1) .compose(HttpClient.observableIoToMain()) .as(HttpClient.bindLifecycle(this)) .subscribe(new HttpDefaultObserver<HttpResult<String>>() { @Override public void start() { } @Override public void success(HttpResult<String> result) { mCollects.put(file.desc, file.desc); } @Override public void fail(HttpError e) { showWhiteFailToast(e.getCode(), e.getMessage(), null); } @Override public void finish() { } }); }
首先,可以将以下代码段:
```
HybridFileParcelable hybridFileParcelable = new HybridFileParcelable(currentPath,
basePath, file.desc, file.permissions, file.date,
file.longSize, file.isDirectory, file.getMode(), "");
if (!TextUtils.isEmpty(file.title)) {
hybridFileParcelable.setName(file.title);
}
```
替换为以下代码:
```
HybridFileParcelable hybridFileParcelable = new HybridFileParcelable(currentPath,
basePath, TextUtils.isEmpty(file.title) ? file.desc : file.title, file.permissions, file.date,
file.longSize, file.isDirectory, file.getMode(), "");
```
这样可以避免在判断 file.title 是否为空时多次调用 TextUtils.isEmpty() 方法。
其次,可以将 Map 的初始化改为以下方式:
```
Map<String, Object> map = new HashMap<>(2);
```
这样可以避免后续 put 操作导致 Map 扩容。
最后,建议对网络请求进行异常捕获处理,避免出现异常时程序崩溃。可以将以下代码:
```
api.saveFileBrowseRecord(body, 1)
.compose(HttpClient.observableIoToMain())
.as(HttpClient.bindLifecycle(this))
.subscribe(new HttpDefaultObserver<HttpResult<String>>() {
@Override
public void start() {
}
@Override
public void success(HttpResult<String> result) {
mCollects.put(file.desc, file.desc);
}
@Override
public void fail(HttpError e) {
showWhiteFailToast(e.getCode(), e.getMessage(), null);
}
@Override
public void finish() {
}
});
```
替换为以下代码:
```
try {
api.saveFileBrowseRecord(body, 1)
.compose(HttpClient.observableIoToMain())
.as(HttpClient.bindLifecycle(this))
.subscribe(new HttpDefaultObserver<HttpResult<String>>() {
@Override
public void start() {
}
@Override
public void success(HttpResult<String> result) {
mCollects.put(file.desc, file.desc);
}
@Override
public void fail(HttpError e) {
showWhiteFailToast(e.getCode(), e.getMessage(), null);
}
@Override
public void finish() {
}
});
} catch (Exception e) {
e.printStackTrace();
}
```
这样可以捕获网络请求中可能出现的异常,并在程序中进行处理。
@ResponseBody @PutMapping @ApiOperation("修改") @LogMethod( type = 1, voParamIndex = 0 ) public HttpResult<Boolean> update(@RequestBody @Validated({Update.class}) V e) { if (this.isPermitted("update")) { this.beforSave(e, false); this.baseService.updateById((BasePO)e); return HttpResult.success(true); } else { throw new NotPremissionException(); } } public boolean isPermitted(String permitName) { NotCheckPermission notCheckPermission = (NotCheckPermission)this.getClass().getAnnotation(NotCheckPermission.class); if (Objects.nonNull(notCheckPermission)) { return true; } else { String path = super.getRequest().getServletPath(); String namespace = path.split("/")[2]; boolean bool = StpUtil.hasPermission(namespace + ":" + permitName); if (!bool) { LogNamespace annotation = (LogNamespace)this.getClass().getAnnotation(LogNamespace.class); List<String> namespaces = Arrays.asList(annotation.permissions()); Iterator var8 = namespaces.iterator(); while(var8.hasNext()) { String ns = (String)var8.next(); if (StpUtil.hasPermission(ns + ":" + permitName)) { return true; } } } return bool; } }
这段代码是一个Controller的update方法,使用了注解来配置接口的行为。具体注解和对应功能如下:
1. @ResponseBody:用于将返回值转换为JSON格式并返回给客户端。
2. @PutMapping:将HTTP请求映射到更新资源的操作。
3. @ApiOperation:Swagger注解,用于描述接口的功能。
4. @LogMethod:自定义注解,用于记录接口的调用日志。type表示日志类型,voParamIndex表示VO对象在方法参数中的索引位置。
接口的主要逻辑如下:
1. 判断当前用户是否有更新权限,如果没有则抛出NotPremissionException异常。
2. 调用beforSave方法,用于在保存前对VO对象进行处理。
3. 调用baseService.updateById方法,更新数据库中对应的记录。
4. 返回HttpResult.success(true),表示更新成功。
isPermitted方法用于判断当前用户是否有指定权限,具体逻辑如下:
1. 判断当前Controller是否有NotCheckPermission注解,如果有则直接返回true。
2. 获取当前请求的路径,并从中提取出命名空间。
3. 判断当前用户是否有命名空间+权限名称的权限,如果有则返回true。
4. 如果没有,则从LogNamespace注解中获取所有权限名称,并循环判断是否有对应权限,如果有则返回true。
5. 如果以上都没有匹配到,则返回false。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![.zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)