字符串判空错误
前端传来的请求数据,若用只用String为null判断,则忽略了str=“”的情况,此时str不空,但str.length()==0
@RequestMapping(path = "/add", method = RequestMethod.POST)
@ResponseBody
public String addDiscussPost(String title, String content){
// 判空
if(title==null){
return CommunityUtil.getJSONString(1,"帖子标题不可为空");
}
if(content==null){
return CommunityUtil.getJSONString(2,"帖子内容不可为空");
}
... ...
return CommunityUtil.getJSONString(0,"发布成功!");
}
引入lang3包,利用StringUtils.isBlank()方法,可以兼顾str==null和str=“”的情况
@RequestMapping(path = "/add", method = RequestMethod.POST)
@ResponseBody
public String addDiscussPost(String title, String content){
// 判空
if(StringUtils.isBlank(title)){
return CommunityUtil.getJSONString(1,"帖子标题不可为空");
}
if(StringUtils.isBlank(content)){
return CommunityUtil.getJSONString(2,"帖子内容不可为空");
}
... ...
return CommunityUtil.getJSONString(0,"发布成功!");
}