Bean验证+ JAX-RS不读取自定义ValidationMessages.properties

时间:2017-11-17 15:57:22

标签: java internationalization jax-rs bean-validation

我正在尝试使用Bean Validation使用自定义消息,但JAX-RS资源不会返回我的自定义消息。我做错了什么?

ValidationMessages.properties

invoice.value.notnull=Invoice value must be informed.

该文件位于src/main/resources

enter image description here

InvoiceResource.java

@Path("/invoice")
public class InvoiceResource {

    @POST
    public void post(@Valid InvoiceRequest request) {
        /* stuff */
    }

}

InvoiceRequest.java

public class InvoiceRequest {

    @NotNull(message = "invoice.value.notnull")
    private Double value;

}

1 个答案:

答案 0 :(得分:0)

发现问题是我在bean参数中声明了消息。消息ID必须在大括号"{ ... }"

之间分叉
@NotNull(message = "{invoice.value.notnull}")

我们如何使用正确的消息获得正确的JSON响应:

{
    "exception": null,
    "fieldViolations": [],
    "propertyViolations": [],
    "classViolations": [],
    "parameterViolations": [
        {
            "constraintType": "PARAMETER",
            "path": "post.arg0.value",
            "message": "Invoice value must be informed",
            "value": ""
        }
    ],
    "returnValueViolations": []
}