请问这段代码有什么问题?完全看不出有什么异常。。

2018-12-24 15:02:27 +08:00
 momuou
4846 次点击
所在节点    程序员
45 条回复
annielong
2018-12-24 16:21:02 +08:00
反正个人写条件两种用 if,三种以上就换 switch,不过返回值提示这种可以定义字典,或者数组,可以统一维护
momuou
2018-12-24 16:21:55 +08:00
@james2013 这种写一两个还好,写多了容易烦躁
momuou
2018-12-24 16:22:57 +08:00
不会贴图,所以没什么贴上来没什么格式。。感谢提建议的各位。
pipicat
2018-12-24 18:24:29 +08:00
写了一段,供你参考。
public class Test {

class Model {
@Warning("业务类型不明,请联系管理员")
public String countTypeId;
@Warning("请选择确切日期进行查询后再进行审核")
public String dealDate;
@Warning("请选择确切地市进行查询后再进行审核")
public String regionCode;
@Warning("选择的支付类型有误,请稍后重试")
public String payType;
}


public void Test(){

Model model = new Model();
HashMap<String,String> result = new HashMap<>();
HashMap<String,String> paramMap = new HashMap<>();

Field[] declaredFields = model.getClass().getDeclaredFields();
boolean pass = true;

for (Field field:declaredFields){
if (field.getAnnotation(Warning.class)!=null){
field.setAccessible(true);
try {
Object o = field.get(model);

if (!isVaild(String.valueOf(o))) {
pass = false;
result.put(field.getName(),field.getAnnotation(Warning.class).value());
break;
} else {
paramMap.put(field.getName(),String.valueOf(o).trim());
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}

if (pass) {
try {
int i = dataRecountService.updateDataPayType(paramMap);
if (i > 0) {
result.put("status", "1");
result.put("msg", "提交成功");
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
result.put("msg", "网络异常,请稍后再尝试。");
}
} else {

}

}

public boolean isVaild(String str){
return StringUtils.isBlank(str) && !"null".equals(str);
}

}

@Target(FIELD)
@Retention(RUNTIME)
@interface Warning{
String value() default "";
}

具体细节可能还要调一下。
lihongjie0209
2018-12-24 18:38:26 +08:00
<iframe
src="https://carbon.now.sh/embed/?bg=rgba(171%2C%20184%2C%20195%2C%201)&t=seti&wt=none&l=text%2Fx-java&ds=true&dsyoff=20px&dsblur=68px&wc=true&wa=true&pv=56px&ph=56px&ln=false&fm=Hack&fs=14px&lh=133%25&si=false&code=%257B%250A%250A%250A%2509%2509%252F%252F%2520%25E4%25B8%259A%25E5%258A%25A1%25E7%25B1%25BB%25E5%259E%258B%250A%2509%2509if%2520(StringUtils.isNotBlank(countTypeId)%2520%2526%2526%2520!%2522null%2522.equals(countTypeId))%2520%257B%250A%2509%2509%2509paramMap.put(%2522countTypeId%2522%252C%2520countTypeId.trim())%253B%250A%252F%252F%2520%25E6%2597%25A5%25E6%259C%259F%250A%2509%2509%2509if%2520(StringUtils.isNotBlank(dealDate)%2520%2526%2526%2520!%2522null%2522.equals(dealDate))%2520%257B%250A%2509%2509%2509%2509paramMap.put(%2522dealDate%2522%252C%2520dealDate.trim())%253B%250A%252F%252F%25E5%259C%25B0%25E5%25B8%2582%25E7%25BC%2596%25E7%25A0%2581%250A%2509%2509%2509%2509if%2520(StringUtils.isNotBlank(regionCode)%2520%2526%2526%2520!%2522null%2522.equals(regionCode))%2520%257B%250A%2509%2509%2509%2509%2509paramMap.put(%2522regionCode%2522%252C%2520regionCode.trim())%253B%250A%2509%2509%2509%2509%2509if%2520(StringUtils.isNotBlank(payType)%2520%2526%2526%2520!%2522null%2522.equals(payType))%2520%257B%250A%252F%252F-%25E6%2594%25AF%25E4%25BB%2598%25E7%25B1%25BB%25E5%259E%258B%25EF%25BC%2588%25200%2520%25E5%25BE%2585%25E7%25A1%25AE%25E8%25AE%25A4%25201%2520%25E9%2580%259A%25E8%25BF%2587%25202%2520%25E4%25B8%258D%25E9%2580%259A%25E8%25BF%2587%250A%2509%2509%2509%2509%2509%2509paramMap.put(%2522payType%2522%252C%2520payType.trim())%253B%250A%2509%2509%2509%2509%2509%2509try%2520%257B%250A%2509%2509%2509%2509%2509%2509%2509int%2520i%2520%253D%2520dataRecountService.updateDataPayType(paramMap)%253B%250A%2509%2509%2509%2509%2509%2509%2509if%2520(i%2520%253E%25200)%2520%257B%250A%2509%2509%2509%2509%2509%2509%2509%2509result.put(%2522status%2522%252C%2520%25221%2522)%253B%250A%2509%2509%2509%2509%2509%2509%2509%2509result.put(%2522msg%2522%252C%2520%2522%25E6%258F%2590%25E4%25BA%25A4%25E6%2588%2590%25E5%258A%259F%2522)%253B%250A%2509%2509%2509%2509%2509%2509%2509%257D%250A%2509%2509%2509%2509%2509%2509%257D%2520catch%2520(Exception%2520e)%2520%257B%250A%2509%2509%2509%2509%2509%2509%2509logger.error(e.getMessage()%252C%2520e)%253B%250A%2509%2509%2509%2509%2509%2509%2509result.put(%2522msg%2522%252C%2520%2522%25E7%25BD%2591%25E7%25BB%259C%25E5%25BC%2582%25E5%25B8%25B8%25EF%25BC%258C%25E8%25AF%25B7%25E7%25A8%258D%25E5%2590%258E%25E5%2586%258D%25E5%25B0%259D%25E8%25AF%2595%25E3%2580%2582%2522)%253B%250A%2509%2509%2509%2509%2509%2509%257D%250A%2509%2509%2509%2509%2509%257D%2520else%2520%257B%250A%2509%2509%2509%2509%2509%2509result.put(%2522msg%2522%252C%2520%2522%25E9%2580%2589%25E6%258B%25A9%25E7%259A%2584%25E6%2594%25AF%25E4%25BB%2598%25E7%25B1%25BB%25E5%259E%258B%25E6%259C%2589%25E8%25AF%25AF%25EF%25BC%258C%25E8%25AF%25B7%25E7%25A8%258D%25E5%2590%258E%25E9%2587%258D%25E8%25AF%2595%2522)%253B%250A%2509%2509%2509%2509%2509%257D%250A%2509%2509%2509%2509%257D%2520else%2520%257B%250A%2509%2509%2509%2509%2509result.put(%2522msg%2522%252C%2520%2522%25E8%25AF%25B7%25E9%2580%2589%25E6%258B%25A9%25E7%25A1%25AE%25E5%2588%2587%25E5%259C%25B0%25E5%25B8%2582%25E8%25BF%259B%25E8%25A1%258C%25E6%259F%25A5%25E8%25AF%25A2%25E5%2590%258E%25E5%2586%258D%25E8%25BF%259B%25E8%25A1%258C%25E5%25AE%25A1%25E6%25A0%25B8%2522)%253B%250A%2509%2509%2509%2509%257D%250A%2509%2509%2509%257D%2520else%2520%257B%250A%2509%2509%2509%2509result.put(%2522msg%2522%252C%2520%2522%25E8%25AF%25B7%25E9%2580%2589%25E6%258B%25A9%25E7%25A1%25AE%25E5%2588%2587%25E6%2597%25A5%25E6%259C%259F%25E8%25BF%259B%25E8%25A1%258C%25E6%259F%25A5%25E8%25AF%25A2%25E5%2590%258E%25E5%2586%258D%25E8%25BF%259B%25E8%25A1%258C%25E5%25AE%25A1%25E6%25A0%25B8.%2522)%253B%250A%2509%2509%2509%257D%250A%2509%2509%257D%2520else%2520%257B%250A%2509%2509%2509result.put(%2522msg%2522%252C%2520%2522%25E4%25B8%259A%25E5%258A%25A1%25E7%25B1%25BB%25E5%259E%258B%25E4%25B8%258D%25E6%2598%258E%25EF%25BC%258C%25E8%25AF%25B7%25E8%2581%2594%25E7%25B3%25BB%25E7%25AE%25A1%25E7%2590%2586%25E5%2591%2598%2522)%253B%250A%2509%2509%257D%250A%250A%2509%257D&es=2x&wm=false"
style="transform:scale(0.7); width:1024px; height:473px; border:0; overflow:hidden;"
sandbox="allow-scripts allow-same-origin">
</iframe>
lihongjie0209
2018-12-24 19:02:12 +08:00
lihongjie0209
2018-12-24 19:07:29 +08:00
laoyur
2018-12-24 19:07:36 +08:00
看到这样排版还肯帮忙的,是真的热心肠了
lihongjie0209
2018-12-24 19:10:03 +08:00
@laoyur 主要是想试一下 gif 录制工具 还有 在 v2 回复图片, 后者看来失败了
devilyaos
2018-12-24 21:28:18 +08:00
counttypeid 为 null (有空格)会进下面的逻辑吧?此时应该条件匹配不上?或者 null 是大写,大小写匹配可以用 equalsignore 嘛……
iceheart
2018-12-25 08:42:27 +08:00
//抛块砖头,帮你重构了一下,java 语法不太熟,将就着写吧
//重复的动作首先要封装一下
bool check(string typeName string typeValue) {
if (StringUtils.isBlank(typeValue) || "null".equals(typeValue)) {
return false;
}
paramMap.put(typeName, typeValue.trim());
return true;
}
...
//try catch 可以简化分支处理
try {
if (!check(countTypeId)) throw Exception("your message");
if (!check(dealDate)) throw Exception("your message");
if (!check(regionCode)) throw Exception("your message");
if (!check(payType)) throw Exception("your message");
int i = 0;
try {
i = dataRecountService.updateDataPayType(paramMap);
}catch (Exception e) {
logger.error(e.getMessage(), e);
throw Exception("网络异常,请稍后再尝试。" );
}
if (i > 0) {
result.put("status", "1");
result.put("msg", "提交成功");
}
}cache(Exception e) {
result.put(e.getMessage());
}
momuou
2018-12-25 09:14:32 +08:00
@pipicat 谢谢
momuou
2018-12-25 09:14:44 +08:00
momuou
2018-12-25 09:16:06 +08:00
@devilyaos 其实判断不是重点。。重点就是怎么优化这种多个判断的,不过还是很感谢
momuou
2018-12-25 09:18:00 +08:00
@iceheart 谢谢,抛异常这种考虑过,不过也是感觉太繁琐
tingfang
2018-12-25 09:34:51 +08:00
这种 ifelse 建议使用卫语句。
tingfang
2018-12-25 09:36:31 +08:00
if(!conditionA){
return;
}
if(!conditionB){
return;
}
if(!conditionC){
return;
}
...
return;
icop
2018-12-25 09:43:54 +08:00
debug 不行吗? IDEA 的 DEBUG 啊。
aa514758835
2018-12-25 09:56:59 +08:00
这是 java 吗~ 我看不懂
wuchenl
2018-12-25 10:12:35 +08:00
使用 equals 的时候,记得考虑空格的情况下。即.trim()。
然后针对这种多个参数要判断是否为空的情况,建议弄一个 BO,接收。然后上面加注解等进行校验。这种可读性太差了。
有 @NotNull,@NotEmpty 注解,搭配 message 属性使用即可;

@NotEmpty(message="证件号不能为空!")
private String certNo;


具体使用时 Controller 层使用 @Valid 注解也可,方法内部进行校验也可。详细的可以自己了解下哦=0=
public String test(@Valid User user){

}

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://tanronggui.xyz/t/520478

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX