gpt4 book ai didi

java - api中如何定义接口(interface)才合理?抛出异常或者返回ErrorCodeBean结果?

转载 作者:太空宇宙 更新时间:2023-11-04 11:40:07 26 4
gpt4 key购买 nike

api中如何定义接口(interface)才合理?抛出异常或者返回ErrorCodeBean结果?示例如下:

1.1 接口(interface)

Dto checkAndSetInfoForApply(String domain, String orderNo); 

1.2 Dto

public class GroupBuyProductResultDto implements Serializable {
private static final long serialVersionUID = 7942969338660852183L;
private Integer personNum;// 部分选中时必须人数
private ResultTypeEnum resultTypeEnum;//接口返回类型
private String msg;//接口返回信息

}

1.3 结果类型枚举

public enum ResultTypeEnum {
SUCCESS(1,"成功"),
FAIL(2,"失败"),
EXCEPTION(3,"接口异常"),
EXT(4,"扩展");

private int code;
private String desc;

ResultTypeEnum(int code, String desc){
this.code = code;
this.desc = desc;
}

}

1.4 转账

Dto groupBuyDto = coreservGrouBuyProductService.checkAndSetInfoForApply(domain, orderNo);
ResultTypeEnum resultTypeEnum = groupBuyDto.getResultTypeEnum();
switch (resultTypeEnum) {
case EXCEPTION:
throw new GroupBuyException("interface error");
case FAIL:
break;
case SUCCESS:
do somthing...
default:
break;
}

2.1界面

Dto checkAndSetInfoForApply(String domain, String orderNo) throws Exception;

2.2 Dto

public class GroupBuyProductResultDto implements Serializable {
private static final long serialVersionUID = 7942969338660852183L;
private Integer personNum;// 部分选中时必须人数

2.3 转账

try{
Dto groupBuyDto = coreservGrouBuyProductService.checkAndSetInfoForApply(domain, orderNo);
}catch(Exception e){
wrong...
}

哪种方式更合适(1或2)?

最佳答案

在你的场景中,第二种实现更好,推荐的方式是从服务返回一个dto或异常dto到 Controller ,你应该为所有异常定义统一的异常处理 Controller ,你可以引用 spring-boot 的异常处理程序的代码。示例如下所示,

@ControllerAdvice(注释= RestController.class) 公共(public)类 RestExceptionHandler {

private static final Logger LOGGER = LoggerFactory.getLogger(RestExceptionHandler.class);

@ExceptionHandler
@ResponseBody
//@ResponseStatus(HttpStatus.BAD_REQUEST)
private RestResult runtimeExceptionHandler(Exception ex) {
LOGGER.error("---------> error!", ex);
return RestResultBuilder.builder().failure().build();
}

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseBody
//@ResponseStatus(HttpStatus.BAD_REQUEST)
private RestResult illegalParamsExceptionHandler(MethodArgumentNotValidException ex) {
BindingResult bindingResult = ex.getBindingResult();
Map<String, String> errors = Maps.newHashMapWithExpectedSize(bindingResult
.getFieldErrorCount());
for (FieldError fieldError : bindingResult.getFieldErrors()) {
errors.put(fieldError.getField(), fieldError.getDefaultMessage());
}
LOGGER.warn("---------> invalid request! fields ex:{}", JSON.toJSONString(errors));
return RestResultBuilder.builder().errorCode(GlobalErrorCode.BAD_REQUEST).data
(errors).build();
}

@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseBody
//@ResponseStatus(HttpStatus.BAD_REQUEST)
public RestResult messageNotReadableExceptionHandler(HttpMessageNotReadableException ex) {
LOGGER.warn("---------> json convert failure, exception:{}", ex.getMessage());
return RestResultBuilder.builder().errorCode(GlobalErrorCode.BAD_REQUEST).build();
}

@ExceptionHandler(MethodArgumentTypeMismatchException.class)
//@ResponseStatus(HttpStatus.NOT_FOUND)
@ResponseBody
public RestResult methodArgumentTypeMismatchExceptionHandler(MethodArgumentTypeMismatchException ex) {
LOGGER.error("---------> path variable, exception:{}", ex.getMessage());
return RestResultBuilder.builder().errorCode(GlobalErrorCode.BAD_REQUEST).message(ex.getMessage()).build();
}

}

关于java - api中如何定义接口(interface)才合理?抛出异常或者返回ErrorCodeBean结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42896346/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com