如何写好一个接口/方法

如何写好一个接口/方法

入参:方法入参学会复用,将可能多次需要使用到的参数抽出来,通过extends提高类的复用性
示例:

注意事项:
1、判空
@NotNull element,基本类型推荐使用
The annotated element must not be null. Accepts any type.
@NotBlank string,字符串类型推荐使用
Validate that the annotated string is not null or empty. The difference to NotEmpty is that trailing whitespaces are getting ignored.
@NotEmpty string, collection, map or array,数组推荐使用
Asserts that the annotated string, collection, map or array is not null or empty.
2、@ToString(callSuper = true) 在输出中包含超类实现toString的结果
Generates an implementation for the toString method inherited by all objects, consisting of printing the values of relevant fields.
Include the result of the superclass’s implementation of toString in the output. default: false
3、implements Serializable 序列化
private static final long serialVersionUID = -4216704193926744650L;
4、对于不需要传递的值可以不写,避免与前端联调时出现误传等问题
5、前期与前端协调好数据类型,方便后期联调
6、当返回参数中定义的有实体类时,返回给前端参数包含class对象,如下图

@Data
public class ConfigPageQueryResponse implements Serializable {
    private static final long serialVersionUID = 8844441658767453697L;
    /**
     * 总条数
     */
    private Long totalCount;

    /**
     * 总页数
     */
    private int totalPage;

    /**
     * 数据列表
     */
    private List<ConfigDTO> dataList;

}

在这里插入图片描述

@Data
public abstract class AbstractAggregateRequest implements Serializable {
    /**
     * 日志号不能为空
     */
    @NotBlank(message = "日志号不能为空")
    private String traceLogId;

}


@Data
@ToString(callSuper = true)
public class OssConfigCreateRequest extends AbstractAggregateRequest{

    private static final long serialVersionUID = -4216704193926744650L;
    /**
     * 编号
     */
    @NotBlank(message = "编号不能为空")
    private String number;
    /**
     * 名称
     */
    @NotBlank(message = "名称不能为空")
    private String name;
}
日志

1、保持日志号全程统一,不要中途更换日志号,日志号可以让前端必传
2、注意项目中已有的日志切面,不要重复写log语句

log.info("call configServiceImpl configCreate param:{}", GsonUtil.getGsonString(configCreateRequest));
判空

1、对于可以为空的属性要注意判空,比较重点的如字符串、日期、数组
CollectionUtils.isEmpty() 数组判空
StringUtils.isNotEmpty() 字符串判空
尤其注意字符串转日期和日期转字符串的时候要注意判空,否则容易出现空指针异常

private static final String TRADE_DATE_PATTERN = "yyyy-MM-dd";
public static Date parseOriginalDate(String requestTime) {
        Date date = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TRADE_DATE_PATTERN);
        try {
            date = simpleDateFormat.parse(requestTime);
        } catch (ParseException e) {
            log.info("时间转换异常{}", e.getMessage());
        }
        return date;
    }

    public static String getSettleDate(Date requestTime) {
        String dateString = "";
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(TRADE_DATE_PATTERN);
            dateString = simpleDateFormat.format(requestTime);
        } catch (Exception e) {
            log.info("时间转换异常{}", e.getMessage());
        }
        return dateString;
    }
异常

1、对于可能出现问题的地方需要抛出异常
2、对于返回的result进行判断

if (result.isSuccess() && result.getResult() != null) {
            return result.getResult();
        }
throw new MyException(MyErrorCode.DATA_INSERT_ERROR, result.getErrorMsg());
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值