下面列出了怎么用javax.xml.bind.ValidationException的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Convert the given {@code JAXBException} to an appropriate exception
* from the {@code org.springframework.oxm} hierarchy.
* @param ex {@code JAXBException} that occurred
* @return the corresponding {@code XmlMappingException}
*/
protected XmlMappingException convertJaxbException(JAXBException ex) {
if (ex instanceof ValidationException) {
return new ValidationFailureException("JAXB validation exception", ex);
}
else if (ex instanceof MarshalException) {
return new MarshallingFailureException("JAXB marshalling exception", ex);
}
else if (ex instanceof UnmarshalException) {
return new UnmarshallingFailureException("JAXB unmarshalling exception", ex);
}
else {
// fallback
return new UncategorizedMappingException("Unknown JAXB exception", ex);
}
}
/**
* Convert the given {@code JAXBException} to an appropriate exception from the
* {@code org.springframework.oxm} hierarchy.
* @param ex {@code JAXBException} that occurred
* @return the corresponding {@code XmlMappingException}
*/
protected XmlMappingException convertJaxbException(JAXBException ex) {
if (ex instanceof ValidationException) {
return new ValidationFailureException("JAXB validation exception", ex);
}
else if (ex instanceof MarshalException) {
return new MarshallingFailureException("JAXB marshalling exception", ex);
}
else if (ex instanceof UnmarshalException) {
return new UnmarshallingFailureException("JAXB unmarshalling exception", ex);
}
else {
// fallback
return new UncategorizedMappingException("Unknown JAXB exception", ex);
}
}
/**
* Convert the given {@code JAXBException} to an appropriate exception from the
* {@code org.springframework.oxm} hierarchy.
* @param ex {@code JAXBException} that occured
* @return the corresponding {@code XmlMappingException}
*/
protected XmlMappingException convertJaxbException(JAXBException ex) {
if (ex instanceof ValidationException) {
return new ValidationFailureException("JAXB validation exception", ex);
}
else if (ex instanceof MarshalException) {
return new MarshallingFailureException("JAXB marshalling exception", ex);
}
else if (ex instanceof UnmarshalException) {
return new UnmarshallingFailureException("JAXB unmarshalling exception", ex);
}
else {
// fallback
return new UncategorizedMappingException("Unknown JAXB exception", ex);
}
}
/**
* Invoked by JAXB after all elements have been unmarshalled. At this point we should have the
* coordinate system (CS). The CS information is required by {@code createConversionFromBase(…)}
* in order to create a {@link MathTransform} with correct axis swapping and unit conversions.
*/
private void afterUnmarshal(Unmarshaller unmarshaller, Object parent) throws ValidationException {
String property = "conversion";
if (conversionFromBase != null) {
final SingleCRS baseCRS = CC_Conversion.setBaseCRS(conversionFromBase, null); // Clear the temporary value now.
property = "coordinateSystem";
if (super.getCoordinateSystem() != null) {
property = "baseCRS";
if (baseCRS != null) {
conversionFromBase = createConversionFromBase(null, baseCRS, conversionFromBase);
return;
}
}
}
/*
* If we reach this point, we failed to update the conversion. The 'baseCRS' information will be lost
* and call to 'getConversionFromBase()' will throw a ClassCastException if this instance is actually
* a ProjectedCRS (because of the method overriding with return type covariance).
*/
throw new ValidationException(Errors.format(Errors.Keys.MissingValueForProperty_1, property));
}
/**
* Validates the supplier.
*
* @param supplier - Supplier that needs to be validated.
* @return true if supplier has passed validation check. False otherwise.
*/
public static boolean validate(Supplier<?> supplier) {
for (Validate annotation
: supplier.getClass().getAnnotationsByType(Validate.class)) {
try {
annotation.value().validate(supplier);
} catch (ValidationException e) {
System.out.println(annotation.description());
e.printStackTrace();
return false;
}
}
return true;
}
public static void validate(Iterable<AttributeDefinition> definition, AttributeMap attributes) throws ValidationException {
Set<AttributeKey<?>> missing = new HashSet<>();
for(AttributeDefinition attribute: definition) {
if(attribute.isOptional()) {
continue;
}
if(attributes.containsKey(attribute.getKey())) {
continue;
}
missing.add(attribute.getKey());
}
if(!missing.isEmpty()) {
throw new ValidationException("Missing required attribute(s): " + missing);
}
}
/**
* Validates the supplier.
*
* @param supplier - Supplier that needs to be validated.
* @return true if supplier has passed validation check. False otherwise.
*/
public static boolean validate(Supplier<?> supplier) {
for (Validate annotation
: supplier.getClass().getAnnotationsByType(Validate.class)) {
try {
annotation.value().validate(supplier);
} catch (ValidationException e) {
System.out.println(annotation.description());
e.printStackTrace();
return false;
}
}
return true;
}
/**
* 认证
*
* @param method 方法
* @param target 目标对象
* @param arguments 参数
* @throws ValidationException
*/
public static void validate(final Method method, final Object target, final Object... arguments) throws
ValidationException {
if (method == null || target == null || arguments == null || arguments.length == 0) {
return;
}
// 获取方法验证
MethodValidation valid = getMethodValidation(method, target);
if (valid != null && valid.validation != null) {
Validator validator;
// 遍历参数
for (int i = 0; i < valid.types.length; i++) {
if (valid.annotations[i] != null) {
// 遍历声明
for (Annotation annotation : valid.annotations[i]) {
// 获取验证器
validator = getValidator(annotation);
// 判断是否能验证
if (validator != null) {
// 验证
validator.validate(target, annotation,
new Validator.Value(valid.names[i], valid.types[i], arguments[i]));
}
}
}
}
}
}
/**
* Validates the supplier.
*
* @param supplier - Supplier that needs to be validated.
* @return true if supplier has passed validation check. False otherwise.
*/
public static boolean validate(Supplier<?> supplier) {
for (Validate annotation
: supplier.getClass().getAnnotationsByType(Validate.class)) {
try {
annotation.value().validate(supplier);
} catch (ValidationException e) {
System.out.println(annotation.description());
e.printStackTrace();
return false;
}
}
return true;
}
/**
* Validates the supplier.
*
* @param supplier - Supplier that needs to be validated.
* @return true if supplier has passed validation check. False otherwise.
*/
public static boolean validate(Supplier<?> supplier) {
for (Validate annotation
: supplier.getClass().getAnnotationsByType(Validate.class)) {
try {
annotation.value().validate(supplier);
} catch (ValidationException e) {
System.out.println(annotation.description());
e.printStackTrace();
return false;
}
}
return true;
}
/**
* Validates the supplier.
*
* @param supplier - Supplier that needs to be validated.
* @return true if supplier has passed validation check. False otherwise.
*/
public static boolean validate(Supplier<?> supplier) {
for (Validate annotation
: supplier.getClass().getAnnotationsByType(Validate.class)) {
try {
annotation.value().validate(supplier);
} catch (ValidationException e) {
System.out.println(annotation.description());
e.printStackTrace();
return false;
}
}
return true;
}
/**
* Validates the supplier.
*
* @param supplier - Supplier that needs to be validated.
* @return true if supplier has passed validation check. False otherwise.
*/
public static boolean validate(Supplier<?> supplier) {
for (Validate annotation
: supplier.getClass().getAnnotationsByType(Validate.class)) {
try {
annotation.value().validate(supplier);
} catch (ValidationException e) {
System.out.println(annotation.description());
e.printStackTrace();
return false;
}
}
return true;
}
/**
* Validates the supplier.
*
* @param supplier - Supplier that needs to be validated.
* @return true if supplier has passed validation check. False otherwise.
*/
public static boolean validate(Supplier<?> supplier) {
for (Validate annotation
: supplier.getClass().getAnnotationsByType(Validate.class)) {
try {
annotation.value().validate(supplier);
} catch (ValidationException e) {
System.out.println(annotation.description());
e.printStackTrace();
return false;
}
}
return true;
}
/**
* Validates the supplier.
*
* @param supplier - Supplier that needs to be validated.
* @return true if supplier has passed validation check. False otherwise.
*/
public static boolean validate(Supplier<?> supplier) {
for (Validate annotation
: supplier.getClass().getAnnotationsByType(Validate.class)) {
try {
annotation.value().validate(supplier);
} catch (ValidationException e) {
System.out.println(annotation.description());
e.printStackTrace();
return false;
}
}
return true;
}
/**
* Validates the supplier.
*
* @param supplier - Supplier that needs to be validated.
* @return true if supplier has passed validation check. False otherwise.
*/
public static boolean validate(Supplier<?> supplier) {
for (Validate annotation
: supplier.getClass().getAnnotationsByType(Validate.class)) {
try {
annotation.value().validate(supplier);
} catch (ValidationException e) {
System.out.println(annotation.description());
e.printStackTrace();
return false;
}
}
return true;
}
/**
* Validates the supplier.
*
* @param supplier - Supplier that needs to be validated.
* @return true if supplier has passed validation check. False otherwise.
*/
public static boolean validate(Supplier<?> supplier) {
for (Validate annotation
: supplier.getClass().getAnnotationsByType(Validate.class)) {
try {
annotation.value().validate(supplier);
} catch (ValidationException e) {
System.out.println(annotation.description());
e.printStackTrace();
return false;
}
}
return true;
}
/**
* Validates the supplier.
*
* @param supplier - Supplier that needs to be validated.
* @return true if supplier has passed validation check. False otherwise.
*/
public static boolean validate(Supplier<?> supplier) {
for (Validate annotation
: supplier.getClass().getAnnotationsByType(Validate.class)) {
try {
annotation.value().validate(supplier);
} catch (ValidationException e) {
System.out.println(annotation.description());
e.printStackTrace();
return false;
}
}
return true;
}
/**
* Validates the supplier.
*
* @param supplier - Supplier that needs to be validated.
* @return true if supplier has passed validation check. False otherwise.
*/
public static boolean validate(Supplier<?> supplier) {
for (Validate annotation
: supplier.getClass().getAnnotationsByType(Validate.class)) {
try {
annotation.value().validate(supplier);
} catch (ValidationException e) {
System.out.println(annotation.description());
e.printStackTrace();
return false;
}
}
return true;
}
@ExceptionHandler(ValidationException.class)
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public ValidationError handleValidationException(HttpServletRequest req, ValidationException exception) {
return ValidationError.builder().error(exception.getMessage()).build();
}