我有一个 dropwizard 1.0.6 应用程序。我想将所有 JAX-RS 和验证注释放在一个接口(interface)上,然后注册我的资源来实现该接口(interface),类似于:
@Path("/user")
@Produces(MediaType.APPLICATION_JSON)
public interface UserEndpoint {
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
Response createUser(
@FormParam("username") @NotEmpty @Email String username,
@FormParam("password") @Size(min=4) String password);
}
以及各自的资源:
public class UserResource implements UserEndpoint {
public Response createUser(String username, String password) {
//my logic here
}
}
这效果非常好,除了错误响应中的验证消息,这些消息已变为:
createUser.arg0 不是格式正确的电子邮件地址
虽然我希望他们是:
表单字段用户名不是格式正确的电子邮件地址
更新:
如果我将 JAX-RS 和验证注释直接添加到 UserResource
,那么验证消息就是我所期望的:它报告 @FormParam 中指定的参数名称
注释。这就是我想要实现的目标,但是在界面中有注释。
最佳答案
注释不会被继承,除非它们用 @Inherited 注释。 。 根据 Java Language Specification
Annotation inheritance only works on classes (not methods, interfaces, or constructors)
因此,是否要检查父类/接口(interface)的注释取决于想要使用注释的代码。
换句话说,您需要为 Dropwizard 做出贡献,使其能够从父类/接口(interface)中搜索注释
关于java - 界面中的 JAX-RS 验证消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42245610/