Java JAX-RS为所有自定义异常提供一个映射器
我创建了一个自定义异常,用于在出现问题时通知用户。例如,如果用户从前端发送一些不正确的数据,并检查是否在后端深处发生了这种情况,而不是我需要做的只是 抛出新的CustomException(“消息”,406) 带有消息和错误代码。之后,我会立即向前端发送一个带有错误代码和文本消息的响应,我可以将其显示在UI上,例如,作为一个snackbar 自定义异常Java JAX-RS为所有自定义异常提供一个映射器,java,exception,jax-rs,Java,Exception,Jax Rs,我创建了一个自定义异常,用于在出现问题时通知用户。例如,如果用户从前端发送一些不正确的数据,并检查是否在后端深处发生了这种情况,而不是我需要做的只是 抛出新的CustomException(“消息”,406) 带有消息和错误代码。之后,我会立即向前端发送一个带有错误代码和文本消息的响应,我可以将其显示在UI上,例如,作为一个snackbar 自定义异常 public class CustomException extends RuntimeException { private final
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;
}
}
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;
}
}
定制例外标签
@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 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;
}
}
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
}
错误
@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 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;
}
}
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
}
但现在我需要一些额外的功能。对于后端的表单验证,我还需要发送哪个文本字段不正确。因此,我认为最好的方法是创建CustomException的新子异常,并在其中添加一个新属性
CustomFormException
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;
}
}
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;
}
}
错误
@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 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;
}
}
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
}
但是我还需要创建一个新的映射器,因为旧的映射器是为CustomException创建的,在CustomException中没有textField属性
是否可以为所有自定义异常使用一个映射器,而不是为每个自定义异常创建额外映射器?您可以为所有自定义异常使用公共基类,并在异常映射器中使用基类 例如:
公共类MyServiceException扩展了RuntimeException{
私人错误码;
私有布尔消息;
公共消息{
返回自定义消息;
}
公共MyServiceException(错误代码错误代码){
super(errorCode.getMessage());
this.errorCode=错误代码;
}
公共MyServiceException(错误代码错误代码字符串消息){
超级(味精);
customMessage=true;
this.errorCode=错误代码;
}
私有静态最终长serialVersionUID=5212119866822715497L;
公共HttpError getHttpError(){
返回新的HttpError(错误代码);
}
}
公共类DataNotFoundException扩展了MyServiceException{
私有静态最终长serialVersionUID=1L;
公共DataNotFoundException(){
超级(错误代码。未找到数据);
}
}
公共类InternalServerException扩展了MyServiceException{
私有静态最终长serialVersionUID=1L;
公共InternalServerException(){
超级(错误代码。内部服务器错误);
}
}
@提供者
公共类MyServiceExceptionMapper实现ExceptionMapper{
@凌驾
公共响应(MyServiceException异常){
返回Response.status(exception.getHttpError().getErrorCode().getCode())
.entity(exception.getHttpError().getErrorCode().getMessage()).type(“text/plain”).build();
}
}
公共类HttpError{
公共HttpError(错误代码错误代码){
this.errorCode=错误代码;
}
私人错误码;
公共错误代码getErrorCode(){
返回错误码;
}
}
公共枚举错误代码{
内部\u服务器\u错误(
Code.INTERNAL\u SERVER\u错误,Messages.INTERNAL\u SERVER\u错误),未找到数据(Code.DATA\u未找到,
“未找到数据”);
私人内部错误代码;
私有字符串错误消息;
专用错误代码(int错误代码、字符串错误消息){
this.errorCode=错误代码;
this.errorMessage=errorMessage;
}
专用接口代码{
int DATA_NOT_FOUND=404;
int INTERNAL_SERVER_错误=500;
}
公共int getCode(){
返回错误码;
}
公共字符串getMessage(){
返回错误消息;
}
}