Spring Data REST:自定义方法验证
我正在尝试使用Spring Data REST
带@RepositoryRestResource
注释的存储库以及自定义方法的实现。有两种情况:
1)我有REST存储库,注释@RepositoryRestResource
它映射到/users
端点。另外,我将@RestController
其映射到相同的端点。
但是,带有@Valid
注解的方法验证可用于@RestController
方法。例如,这有效:
@ResponseBody@RequestMapping(value = "/users")
public ResponseEntity signUp(@RequestBody @Valid final UserSignUpRequest userSignUpRequest)
2)与REST信息库一起使用的@RepositoryRestController
控制器是控制器。这样,两个方法都在@RepositoryRestController
和@RepositoryRestResource
中声明了。但是
。DATAREST-593已描述了此问题。
有什么想法至少可以解决两个问题之一?主要思想是将@RepositoryRestResource
存储库与自定义控制器方法和注释验证一起使用。
回答:
似乎在这种情况下没有好的解决方案,并且@Valid
默认情况下不以任何方式支持注释,请参阅DATAREST-593。因此,为了支持方法上的@Valid
注释@RepositoryRestController
,我创建了以下@ControllerAdvice
类:
package com.tivoli.api.application.advice;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceAdapter;
import javax.validation.Valid;
import javax.validation.ValidationException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
/**
* Workaround class for making JSR-303 annotation validation work for controller method parameters.
* Check the issue <a href="https://jira.spring.io/browse/DATAREST-593">DATAREST-593</a>
*/
@ControllerAdvice
public class RequestBodyValidationProcessor extends RequestBodyAdviceAdapter {
private final Validator validator;
public RequestBodyValidationProcessor(@Autowired final Validator validator) {
this.validator = validator;
}
@Override
public boolean supports(final MethodParameter methodParameter, final Type targetType, final Class<? extends
HttpMessageConverter<?>> converterType) {
final Annotation[] parameterAnnotations = methodParameter.getParameterAnnotations();
for (final Annotation annotation : parameterAnnotations) {
if (annotation.annotationType().equals(Valid.class)) {
return true;
}
}
return false;
}
@Override
public Object afterBodyRead(final Object body, final HttpInputMessage inputMessage, final MethodParameter
parameter, final Type targetType, final Class<? extends HttpMessageConverter<?>> converterType) {
final Object obj = super.afterBodyRead(body, inputMessage, parameter, targetType, converterType);
final BindingResult bindingResult = new BeanPropertyBindingResult(obj, obj.getClass().getCanonicalName());
validator.validate(obj, bindingResult);
if (bindingResult.hasErrors()) {
throw new ValidationException(createErrorMessage(bindingResult));
}
return obj;
}
private String createErrorMessage(final BindingResult bindingResult) {
final StringBuilder stringBuilder = new StringBuilder("Invalid parameters specified.");
if (bindingResult.getFieldErrors() != null && !bindingResult.getFieldErrors().isEmpty()) {
stringBuilder.append(" Fields:");
bindingResult.getFieldErrors().forEach(fieldError -> stringBuilder
.append(" [ ")
.append(fieldError.getField())
.append(" : ")
.append(fieldError.getRejectedValue())
.append(" ] "));
} else if (bindingResult.getAllErrors() != null && !bindingResult.getAllErrors().isEmpty()) {
final ObjectError objectError = bindingResult.getAllErrors().get(0); // get the first error
stringBuilder.append(" Message: ")
.append(objectError.getDefaultMessage());
}
return stringBuilder.toString();
}
}
以上是 Spring Data REST:自定义方法验证 的全部内容, 来源链接: utcz.com/qa/410146.html