Grails 3 自定义错误对象
Grails 3 Customizing Error object
自定义由 grails 域约束验证失败返回的默认错误对象的最佳方法是什么?
我现在得到的JSON是
{"errors": [{"object": "com.self.learning.grails.demo.api.Person",
"field": "name",
"rejected-value": "K12332434",
"message": "Property [orgKey] of class [class com.self.learning.grails.demo.api.Person] with value [K123324343432432432432] exceeds the maximum size of [12]"
}]}
我想从上面的响应中去掉 "object" 并希望有 "error-code".
我是 grails 的新手,几乎没有基本的实现。提前致谢。
您可以编写自己的 class 并用您想要的数据填充它。还要考虑包括您可能需要的其他数据
您可以使用的示例 BaseException:
public class BaseException extends Exception {
static def userService
//import org.apache.commons.logging.LogFactory
private static final log = LogFactory.getLog(this)
private int status ;
private String devMessage;
private String extendedMessage;
private String moreInfo;
private int errorCode;
boolean error = true;
public BaseException(int status,int errorCode,String message, String extendedMessage ,String moreInfo){
this.errorCode = errorCode;
this.status = status;
this.devMessage = message;
this.extendedMessage = extendedMessage;
this.moreInfo = moreInfo;
}
public JSONObject errorResponse(){
JSONObject errorJson = new JSONObject();
errorJson.put("status",this.status);
errorJson.put("errorCode",this.errorCode);
errorJson.put("message",this.devMessage);
errorJson.put("extendedMessage",this.extendedMessage);
errorJson.put("error",error);
errorJson.put("dateTimeStamp", new Timestamp(new Date().time).toString());
return errorJson;
}
public static BaseException createBaseException(String jsonStr) {
try {
def json = new JsonSlurper().parseText(jsonStr)
return new BaseException(json["status"],json["errorCode"],json["message"], json["extendedMessage"] ,json["moreInfo"])
} catch (Exception ex) {
return null
}
}
}
您可以创建一个新的custom marshaller for validation errors并在Bootstrap.groovy
中注册为
JSON.registerObjectMarshaller( new MyCustomValidationErrorsMarshaller() )
只需将 this line 替换为您的 error-code
例如:
json.property( "error-code", HttpStatus.UNPROCESSABLE_ENTITY.value() )
一种快速的方法是在 bootstrap 中注册对象编组器,但这会膨胀 bootstrap class。编写自定义编组器更干净。
另一种方法是编写 interceptor and intercept response,然后将错误响应中的 object
替换为您想要的错误代码。
自定义由 grails 域约束验证失败返回的默认错误对象的最佳方法是什么?
我现在得到的JSON是
{"errors": [{"object": "com.self.learning.grails.demo.api.Person",
"field": "name",
"rejected-value": "K12332434",
"message": "Property [orgKey] of class [class com.self.learning.grails.demo.api.Person] with value [K123324343432432432432] exceeds the maximum size of [12]"
}]}
我想从上面的响应中去掉 "object" 并希望有 "error-code".
我是 grails 的新手,几乎没有基本的实现。提前致谢。
您可以编写自己的 class 并用您想要的数据填充它。还要考虑包括您可能需要的其他数据
您可以使用的示例 BaseException:
public class BaseException extends Exception {
static def userService
//import org.apache.commons.logging.LogFactory
private static final log = LogFactory.getLog(this)
private int status ;
private String devMessage;
private String extendedMessage;
private String moreInfo;
private int errorCode;
boolean error = true;
public BaseException(int status,int errorCode,String message, String extendedMessage ,String moreInfo){
this.errorCode = errorCode;
this.status = status;
this.devMessage = message;
this.extendedMessage = extendedMessage;
this.moreInfo = moreInfo;
}
public JSONObject errorResponse(){
JSONObject errorJson = new JSONObject();
errorJson.put("status",this.status);
errorJson.put("errorCode",this.errorCode);
errorJson.put("message",this.devMessage);
errorJson.put("extendedMessage",this.extendedMessage);
errorJson.put("error",error);
errorJson.put("dateTimeStamp", new Timestamp(new Date().time).toString());
return errorJson;
}
public static BaseException createBaseException(String jsonStr) {
try {
def json = new JsonSlurper().parseText(jsonStr)
return new BaseException(json["status"],json["errorCode"],json["message"], json["extendedMessage"] ,json["moreInfo"])
} catch (Exception ex) {
return null
}
}
}
您可以创建一个新的custom marshaller for validation errors并在Bootstrap.groovy
中注册为
JSON.registerObjectMarshaller( new MyCustomValidationErrorsMarshaller() )
只需将 this line 替换为您的 error-code
例如:
json.property( "error-code", HttpStatus.UNPROCESSABLE_ENTITY.value() )
一种快速的方法是在 bootstrap 中注册对象编组器,但这会膨胀 bootstrap class。编写自定义编组器更干净。
另一种方法是编写 interceptor and intercept response,然后将错误响应中的 object
替换为您想要的错误代码。