3

I'd like to validate my DTO in my @RepositoryRestController with the javax annotation @Valid. However @RepositoryRestController doesn't currently support @Valid as you can see in this ticket: https://jira.spring.io/browse/DATAREST-593

If I use a @RestController my @Valid would work fine, however then my @RepositoryRestResource wouldn't work anymore. I would need to manually write a method in my @RestController for each functionality (findOne(), findAll() etc.). Currently I can just use the @RepositoryRestResource with a Projection for the methods findAll() etc.

How do I validate the DTOs in a @RepositoryRestController?

Repository:

@RepositoryRestResource(excerptProjection = ChipProjection.class)
public interface ChipRepository extends JpaRepository<Chip, Long> {

}

Projection:

@Projection(name = "summary", types = Chip.class)
public interface ChipProjection {
    Long getId();
    ChipIdentifier getChipIdentifier();
}

Controller:

@RepositoryRestController
public class ChipRestController {
    @Autowired
    ChipService chipService;

    @RequestMapping(value = "/chips", method = RequestMethod.POST)
    public @ResponseBody ChipHelper saveChip(@Valid @RequestBody ChipHelper chip, BindingResult result){
        List<FieldError> errors = result.getFieldErrors();
        //errors is always empty, @Valid not working
        chipService.save(chip);
        return chip;
    }
}

ChipHelper:

@Data
public class ChipHelper {
    @NotNull
    private Long id;

    @NotNull
    @Size(min = 10)
    private String identifier;
}
Jan Meier
  • 138
  • 1
  • 1
  • 9

3 Answers3

2

This answer: https://stackoverflow.com/a/44304198/5435182 works for me:

Just add this to your @RepositoryRestController :

@Inject
private LocalValidatorFactoryBean validator;

@InitBinder
protected void initBinder(WebDataBinder binder) {
    binder.addValidators(validator);
}
marknote
  • 1,058
  • 8
  • 10
1

Seems that there are no good solution in this case and @Valid annotation is not supported by default in any way as stated in DATAREST-593. That why, to support @Valid annotation on @RepositoryRestController methods, I've created the following @ControllerAdvice class:

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();
    }
}
yyunikov
  • 5,719
  • 2
  • 43
  • 78
  • Hy, Yuriy. Your solution works and i think it's the best workaround that i could find nowadays. 3 years have passed and this issue is still actual, unfortunatelly. I would suggest to use `@ControllerAdvice` with 'basePackageClasses' setting to led this Advice be applicable only for specific Controllers (e.g except `@Controllers` & `@RestControllers`) – Bogdan Samondros Aug 01 '18 at 20:38
1

Maybe you could add annotation @Validated to Controller as follows:

@Validated
@RepositoryRestController
public class ChipRestController {
    ...
}

I refer this answer: https://coderanch.com/t/724074/frameworks/Valid-annotation-working-service-method And this do help me:)

Dred
  • 11
  • 2