commonresult
时间: 2024-01-17 07:03:57 浏览: 10
CommonResult 是一个通用的返回结果类,它通常用于封装 RESTful 接口的响应结果。它包含了一个状态码和一个消息体,通常使用 JSON 格式进行返回。
常见的状态码有 200 表示成功,400 表示请求参数错误,401 表示未授权,500 表示服务器错误等。消息体可以是一个对象、一个列表或者是一个简单的字符串,具体取决于接口的返回类型。
使用 CommonResult 可以方便地封装接口的返回结果,同时也可以提高接口的可读性和易用性。在 Spring 框架中,可以通过使用 @RestController 和 @ResponseBody 注解来直接将 CommonResult 对象转换为 JSON 格式进行返回。
相关问题
CommonResult是什么类包含什么属性,要能够.failed
CommonResult是一个通用的返回结果类,通常用于API接口的返回值。它包含以下属性:
- code:返回结果的状态码,通常为一个整数。
- message:返回结果的消息,通常为一个字符串,用于说明返回结果的具体含义。
- data:返回结果的数据,通常为一个对象,用于存储返回结果的具体内容。
如果返回结果的状态码表示操作失败,通常可以通过判断code属性是否为一个失败状态码来确定返回结果是否失败,例如:
```
CommonResult result = someService.doSomething();
if (result.getCode() == -1) {
// 操作失败,需要处理失败情况
// 可以通过result.getMessage()获取失败原因
// 或者通过result.getData()获取失败的具体信息
// ...
} else {
// 操作成功,可以通过result.getData()获取成功的结果数据
// ...
}
```
优化这段代码 ArrayList<String> stnCodeList = new ArrayList<>(Arrays.asList("1020C", "1021C", "1135C", "1136C" , "1137C", "1138C", "1139C" , "1140C" , "1141C" , "1142C" , "1143C" , "1144C" , "1145C" , "1147C" , "1149C" , "1221C" , "1222C" , "1223C" , "1224C","1146C","1119C","1148C")); @GetMapping("stn") public CommonResult airStation(){ HttpResponse response = HttpRequest.get(Constants.SMALL_AIR_STATION).execute(); List<AirStationVo> data = new ArrayList<>(); try { if (response.getStatus() == HttpStatus.HTTP_OK) { String body = response.body(); CommonResult commonResult = JSONUtil.toBean(body, CommonResult.class); List<JSONObject> airStationVos = (List)commonResult.getData(); for (String value : stnCodeList) { for (JSONObject stationVo : airStationVos) { AirStationVo airStationVo = JSONUtil.toBean(stationVo, AirStationVo.class); String stnCode = airStationVo.getStnCode(); String s = value; if (s.equals(stnCode)) { data.add(airStationVo); } } } } }catch (Exception e) { e.printStackTrace(); return CommonResult.failed(e.getMessage()); } return CommonResult.success(data,"成功"); }
首先,可以将 `stnCodeList` 改为 `Set<String>` 类型,这样查找元素的时间复杂度会更低。其次,可以使用流式 API 来简化代码并提高效率。修改后的代码如下:
```
Set<String> stnCodeSet = new HashSet<>(Arrays.asList("1020C", "1021C", "1135C", "1136C", "1137C", "1138C", "1139C", "1140C", "1141C", "1142C", "1143C", "1144C", "1145C", "1147C", "1149C", "1221C", "1222C", "1223C", "1224C", "1146C", "1119C", "1148C"));
@GetMapping("stn")
public CommonResult airStation() {
HttpResponse response = HttpRequest.get(Constants.SMALL_AIR_STATION).execute();
try {
if (response.getStatus() == HttpStatus.HTTP_OK) {
String body = response.body();
CommonResult commonResult = JSONUtil.toBean(body, CommonResult.class);
List<JSONObject> airStationVos = (List)commonResult.getData();
List<AirStationVo> data = airStationVos.stream()
.map(stationVo -> JSONUtil.toBean(stationVo, AirStationVo.class))
.filter(station -> stnCodeSet.contains(station.getStnCode()))
.collect(Collectors.toList());
return CommonResult.success(data, "成功");
}
} catch (Exception e) {
e.printStackTrace();
return CommonResult.failed(e.getMessage());
}
return CommonResult.failed("请求失败");
}
```
使用流式 API 可以简化代码并提高效率。同时,将 `stnCodeList` 改为 `Set<String>` 类型,可以提高查找元素的效率。