gpt4 book ai didi

java - 用于所有自定义异常的 JAX-RS 一个映射器

转载 作者:行者123 更新时间:2023-11-30 12:07:24 25 4
gpt4 key购买 nike

我已经创建了一个自定义异常以在出现问题时通知用户。例如。如果用户从前端发送了一些不正确的数据并检查它是否发生在后端某个深处,那么我需要做的只是为了

throw new CustomException("Message", 406)

带有消息和错误代码。之后会立即向前端发送一个响应,其中包含我可以在 UI 上显示的错误代码和文本消息,例如作为 snackbar 。

自定义异常

public class CustomException extends RuntimeException {

private final int errorCode;

private static final long serialVersionUID = 6220614065727287629L;


public CustomException(String message, int errorCode) {
super(message);
this.errorCode = errorCode;
}

int getErrorCode() {
return errorCode;
}
}

自定义异常映射器

@Provider
public class CustomExceptionMapper implements ExceptionMapper<CustomException> {

@Override
public Response toResponse(CustomException ex) {

ErrorResponse response = new ErrorResponse(ex.getMessage(), ex.getErrorCode());
return Response.status(ex.getErrorCode()).entity(response).build();
}
}

错误响应

公共(public)类 ErrorResponse {

 private Error error;

ErrorResponse(String errorMessage, int errorCode) {
createError(errorMessage, errorCode);
}

private void createError(String message, int code) {
setError(new Error(message, code));
}

public Error getError() {
return error;
}

public void setError(Error error) {
this.error = error;
}

}

错误

public class Error {
private String message;
private int code;

public Error(String message, int code) {
this.message = message;
this.code = code;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}
}

但现在我需要一些额外的功能。对于后端的表单验证,我还需要发送哪个文本字段不正确。所以我认为最好的方法是创建一个新的 CustomException 子异常并在其中添加一个新属性。

自定义表单异常

public class CustomFormException extends CustomException {

private final String formField;

private static final long serialVersionUID = 6220614065727287630L;


public CustomFormException(String message, int errorCode, String formField) {
super(message, errorCode);
this.formField = formField;
}

String getFormField() {
return formField;
}
}

我可以稍微修改一下 ErrorResponse 和 Error 类。添加新构造函数和新属性 textField

public class ErrorResponse {

private Error error;

ErrorResponse(String errorMessage, int errorCode) {
createError(errorMessage, errorCode);
}

ErrorResponse(String errorMessage, int errorCode, String textField) {
createError(errorMessage, errorCode, textField);
}

private void createError(String message, int code) {
setError(new Error(message, code));
}

private void createError(String message, int code, String textField) {
setError(new Error(message, code, textField));
}

public Error getError() {
return error;
}

public void setError(Error error) {
this.error = error;
}

}

错误

public class Error {
private String message;
private int code;
private int String textField;

public Error(String message, int code) {
this.message = message;
this.code = code;
}

public Error(String message, int code, String textField) {
this.message = message;
this.code = code;
this.textField = textField;
}

// setters and getters
}

但是我还需要创建一个新的 Mapper,因为旧的 Mapper 是为 CustomException 制作的,而在 CustomException 中没有 textField 属性。

是否有可能以某种方式为所有自定义异常使用一个映射器,而不是为每个自定义异常创建额外的映射器?

最佳答案

您可以为所有自定义异常使用通用基类,并在异常映射器中使用基类。

例如:

public class MyServiceException extends RuntimeException {

private ErrorCode errorCode;
private boolean customMessage;


public boolean haveCustomMessage() {
return customMessage;
}

public MyServiceException(ErrorCode errorCode) {
super(errorCode.getMessage());
this.errorCode = errorCode;
}

public MyServiceException(ErrorCode errorCode, String msg) {
super(msg);
customMessage = true;
this.errorCode = errorCode;
}

private static final long serialVersionUID = 5212119866822715497L;


public HttpError getHttpError() {

return new HttpError(errorCode);
}
}

public class DataNotFoundException extends MyServiceException {

private static final long serialVersionUID = 1L;

public DataNotFoundException() {
super(ErrorCode.DATA_NOT_FOUND);
}

}

public class InternalServerException extends MyServiceException {

private static final long serialVersionUID = 1L;

public InternalServerException() {
super(ErrorCode.INTERNAL_SERVER_ERROR);
}

}

@Provider
public class MyServiceExceptionMapper implements ExceptionMapper<MyServiceException> {

@Override
public Response toResponse(MyServiceException exception) {
return Response.status(exception.getHttpError().getErrorCode().getCode())
.entity(exception.getHttpError().getErrorCode().getMessage()).type("text/plain").build();
}
}

public class HttpError {

public HttpError(ErrorCode errorCode) {
this.errorCode = errorCode;
}

private ErrorCode errorCode;

public ErrorCode getErrorCode() {
return errorCode;
}

}

public enum ErrorCode {

INTERNAL_SERVER_ERROR(
Code.INTERNAL_SERVER_ERROR, Messages.INTERNAL_SERVER_ERROR), DATA_NOT_FOUND(Code.DATA_NOT_FOUND,
"Data not found ");


private int errorCode;
private String errorMessage;

private ErrorCode(int errorCode, String errorMessage) {
this.errorCode = errorCode;
this.errorMessage = errorMessage;
}

private interface Code {

int DATA_NOT_FOUND = 404;
int INTERNAL_SERVER_ERROR = 500;
}

public int getCode() {
return errorCode;
}

public String getMessage() {
return errorMessage;
}
}

关于java - 用于所有自定义异常的 JAX-RS 一个映射器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54998134/

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