首页 文章

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

提问于
浏览
0

我有一个简单的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对象发送到我的 endpoints 时:

{
    "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

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

相关问题