JAX-RS上的Bean验证不会阻止无效请求

时间:2015-10-20 11:39:07

标签: java jax-rs bean-validation

我有一个简单的JAX-RS服务,用JSR-303注释注释:

@Path("/settings")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@ValidateRequest
public interface SettingsRestService {

    @POST
    SettingsResponse saveSetting (@Valid @NotNull Setting setting);
}

我的Settings课程:

public class Setting {

    @NotNull(message = "Field key must exist.")
    @Pattern(regexp = "^[\\w]+$", 
        message = "Field key must only contain alphanumeric characters and underscores.")
    private String key;

    @NotNull(message = "Field value must exist.")
    @Size(min = 1, message = "The value of the setting must be at least 1 character.")
    private String value;

    ...
}

当我将以下JSON对象发送到我的端点时:

{
    "key": "aKey+",
    "value": "aValue"
}

我收到了这个回复:

{
    exception: null
    fieldViolations: [0]
    propertyViolations: [0]
    classViolations: [0]
    parameterViolations: [1]
      0: {
           constraintType: "PARAMETER"
           path: "SettingsRestService#saveSetting(arg0).key"
           message: "Field key must only contain alphanumeric characters and underscores."
           value: "aKey+"
         }-
      -
    returnValueViolations: [0]
}

即使验证有效,请求仍然会通过服务并由应用程序的其余部分处理。

为什么这不会被阻止的任何想法?我是否需要其他配置来处理验证?

WAR部署在JBoss 6.3.3.GA上,我们使用Hibernate Validator 4.3.2.Final-redhat-1和resteasy-jaxrs-3.0.9.Final。

1 个答案:

答案 0 :(得分:0)

在我们的案例中,似乎拦截器“隐藏”了从重新安排中抛出的异常。我还没有找到“为什么”,但如果我有更新,我会告诉你。