使用@RequestBody和@RequestParam进行Rest API验证错误

时间:2017-07-05 08:09:04

标签: java spring hibernate rest validation

我已经实施了休息服务,并希望对其进行验证。在我的班上,我有@RequestBody和@RequestParam。我想验证这两个对象。我这样做:

@Controller
@Validated
public class RestApiImpl {

    @Autowired
    ClassA classA;

    @ResponseBody
    @RequestMapping(value = "/classA",
            method = RequestMethod.POST,
            produces = {MediaType.APPLICATION_JSON_VALUE},
            consumes = {MediaType.APPLICATION_JSON_VALUE})
    public ClassAResponse getList(@Valid @RequestBody ClassARequest request,
                                               @Min(value = 1, message = "At least 1 object is expected")
                                               @RequestParam(value = "quantity",defaultValue = "3",required = false) String quantity) {
        return (ClassAResponse) classA.executeService(request);
    }
}

public class ClassARequest {

    @NotNull
    @NotEmpty
    @Size(max = 6, message = "tes1")
    private String value1;

    public String getValue1() {
        return value1;
    }

    public void setValue1(String value1) {
        this.value1 = value1;
    }
}

并且帖子返回500内部服务器错误并记录:

09:49:21,240 INFO  [STDOUT] 09:49:21,240 ERROR [[dispatcher]] Servlet.service() for servlet dispatcher threw exception
javax.validation.ConstraintDeclarationException: Only the root method of an overridden method in an inheritance hierarchy may be annotated with parameter constraints, but there are parameter constraints defined at all of the following overridden methods:

当我删除@Validated时,验证@RequestBody并且工作正常,但@RequestParam的验证无法正常工作。如果我从@RequestBody中删除@Valid验证请求不起作用。如何配置它适用于@RequestBody和@RequestParam。或者没有任何解决方案,唯一的方法是将参数数量移动到请求?

spring framework 3.2.18.RELEASE和hibernate-validator 4.2.0.Final(它不能更改为spring和hibernate的更新版本)

0 个答案:

没有答案