在开发时,我们都习惯用gsonformat自动生成javaBean来接收后台返回的数据,然而,很多时候,后台并未返回字段或者该字段为空,我们在调用时就会出现各种Null pointer exception,为了避免这个错误,其实生成get和set方法时,可以自定义模板如下:
本来的模板为:IntelliJ Default
#if($field.modifierStatic)
static ##
#end
$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
#if ($field.boolean && $field.primitive)is##
#elseget##
#end
${name}() {return $field.name;
}
研究发现,set和get方法可以根据&name和field.name来进性判断,于是尝试修改如下:
get方法:
#if($field.modifierStatic)
static ##
#end
$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
#if ($field.boolean && $field.primitive)#if ($StringUtil.startsWithIgnoreCase($name, 'is'))#set($name = $StringUtil.decapitalize($name))#elseis##
#end
#elseget##
#end
${name}() {#if ($field.string)return $field.name == null ? "" : $field.name;#else #if ($field.list)if ($field.name == null) {return new ArrayList<>();}return $field.name;#else return $field.name;#end#end
}
set方法如下:
#set($paramName = $helper.getParamName($field, $project))
public ##
#if($field.modifierStatic)static ##
#end
void set$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))($field.type $paramName) {#if ($field.name == $paramName)#if (!$field.modifierStatic)this.###else$classname.###end#end#if($field.string)$field.name = $paramName == null ? "" : $paramName;#else $field.name = $paramName;#end
}
如此便可生成如下代码:
public class ClassifyBean {private String msg;private int code;private boolean success;private List<DataBean> data;public String getMsg() {return msg == null ? "" : msg;}public void setMsg(String msg) {this.msg = msg == null ? "" : msg;}public int getCode() {return code;}public void setCode(int code) {this.code = code;}public boolean isSuccess() {return success;}public void setSuccess(boolean success) {this.success = success;}public List<DataBean> getData() {if (data == null) {return new ArrayList<>();}return data;}public void setData(List<DataBean> data) {this.data = data;}public static class DataBean {private String id;private String name;private String parentId;private int flag;private String thumbnail;private int isDelete;private int priority;private List<ChildrenBeanX> children;public String getId() {return id == null ? "" : id;}public void setId(String id) {this.id = id == null ? "" : id;}public String getName() {return name == null ? "" : name;}public void setName(String name) {this.name = name == null ? "" : name;}public String getParentId() {return parentId == null ? "" : parentId;}public void setParentId(String parentId) {this.parentId = parentId == null ? "" : parentId;}public int getFlag() {return flag;}public void setFlag(int flag) {this.flag = flag;}public String getThumbnail() {return thumbnail == null ? "" : thumbnail;}public void setThumbnail(String thumbnail) {this.thumbnail = thumbnail == null ? "" : thumbnail;}public int getIsDelete() {return isDelete;}public void setIsDelete(int isDelete) {this.isDelete = isDelete;}public int getPriority() {return priority;}public void setPriority(int priority) {this.priority = priority;}public List<ChildrenBeanX> getChildren() {if (children == null) {return new ArrayList<>();}return children;}public void setChildren(List<ChildrenBeanX> children) {this.children = children;}public static class ChildrenBeanX {private String id;private String name;private String parentId;private int flag;private int isDelete;private int priority;private String thumbnail;private List<ChildrenBean> children;public String getId() {return id == null ? "" : id;}public void setId(String id) {this.id = id == null ? "" : id;}public String getName() {return name == null ? "" : name;}public void setName(String name) {this.name = name == null ? "" : name;}public String getParentId() {return parentId == null ? "" : parentId;}public void setParentId(String parentId) {this.parentId = parentId == null ? "" : parentId;}public int getFlag() {return flag;}public void setFlag(int flag) {this.flag = flag;}public int getIsDelete() {return isDelete;}public void setIsDelete(int isDelete) {this.isDelete = isDelete;}public int getPriority() {return priority;}public void setPriority(int priority) {this.priority = priority;}public String getThumbnail() {return thumbnail == null ? "" : thumbnail;}public void setThumbnail(String thumbnail) {this.thumbnail = thumbnail == null ? "" : thumbnail;}public List<ChildrenBean> getChildren() {if (children == null) {return new ArrayList<>();}return children;}public void setChildren(List<ChildrenBean> children) {this.children = children;}public static class ChildrenBean {private String id;private String name;private String parentId;private int flag;private int isDelete;private int priority;private String thumbnail;private List<?> children;public String getId() {return id == null ? "" : id;}public void setId(String id) {this.id = id == null ? "" : id;}public String getName() {return name == null ? "" : name;}public void setName(String name) {this.name = name == null ? "" : name;}public String getParentId() {return parentId == null ? "" : parentId;}public void setParentId(String parentId) {this.parentId = parentId == null ? "" : parentId;}public int getFlag() {return flag;}public void setFlag(int flag) {this.flag = flag;}public int getIsDelete() {return isDelete;}public void setIsDelete(int isDelete) {this.isDelete = isDelete;}public int getPriority() {return priority;}public void setPriority(int priority) {this.priority = priority;}public String getThumbnail() {return thumbnail == null ? "" : thumbnail;}public void setThumbnail(String thumbnail) {this.thumbnail = thumbnail == null ? "" : thumbnail;}public List<?> getChildren() {if (children == null) {return new ArrayList<>();}return children;}public void setChildren(List<?> children) {this.children = children;}}}}
仅供参考,如有疑问可联系zhang_quan_888@163.com,欢迎指正!