Rob*_*ert 5 jax-rs jersey bean-validation
我在Glasshfish 5中运行一个Web应用程序,它使用Jersey JAX-RS提供REST端点.它还使用bean验证.我遇到的问题是任何以"get"开头的方法总是在返回具有@Valid注释的东西时被调用.
例:
@Path("/hello")
public class HelloResource {
@GET
public @Valid HelloMessage getSomething() {
HelloMessage helloMessage = new HelloMessage();
helloMessage.setMessage("Hello World!");
return helloMessage;
}
@POST
public @Valid HelloMessage updateMessage(@Valid HelloMessage message) {
return message;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我对/ hello执行POST,您将看到在调用updateMessage之前调用getSomething方法.如果我在getSomething方法的返回类型上删除@Valid注释,则不会调用getSomething.
这是根据规格吗?你基本上不会在REST类中命名以"get"开头的方法吗?
在过去,我已经在github上报告了这个问题,但从未收到回复.
https://github.com/eclipse-ee4j/jersey/issues/3743
其他课程:
@ApplicationPath("/")
public class HelloApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
return Collections.singleton(HelloResource.class);
}
}
public class HelloMessage {
@Size(max = 100)
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
Run Code Online (Sandbox Code Playgroud)
最小项目可在https://github.com/robertatgh/stackoverflow-50658396/tree/develop找到
由于命名约定,这变成了一个有趣的问题。调试球衣源代码你会发现它通过了
org.glassfish.jersey.server.validation.internal.DefaultConfiguredValidator.onValidate(ValidationInterceptorContext)行:166
public void onValidate(final ValidationInterceptorContext ctx) {
final Object resource = ctx.getResource();
final Invocable resourceMethod = ctx.getInvocable();
final Object[] args = ctx.getArgs();
final Set<ConstraintViolation<Object>> constraintViolations = new HashSet<>();
final BeanDescriptor beanDescriptor = getConstraintsForClass(resource.getClass());
// Resource validation.
if (beanDescriptor.isBeanConstrained()) {
constraintViolations.addAll(validate(resource));
}
if (resourceMethod != null
&& configuration.getBootstrapConfiguration().isExecutableValidationEnabled()) {
final Method handlingMethod = resourceMethod.getHandlingMethod();
Run Code Online (Sandbox Code Playgroud)
有趣的部分是围绕
// Resource validation.
if (beanDescriptor.isBeanConstrained()) {
constraintViolations.addAll(validate(resource));
}
Run Code Online (Sandbox Code Playgroud)
相同的定义是
@Override
public final boolean isBeanConstrained() {
return hasConstraints() || !constrainedProperties.isEmpty();
}
Run Code Online (Sandbox Code Playgroud)
现在,如果你看看constrainedProperties下面显示的它的值
所以它认为这getSomething意味着一个属性something,然后在属性本身上插入验证。
所以现在如果我们像下面这样重命名该方法
@GET
public @Valid HelloMessage doGetSomething() {
System.out.println("* * * *---==** getSomething() called **==---* * * *");
HelloMessage helloMessage = new HelloMessage();
helloMessage.setMessage("H");
return helloMessage;
}
@POST
public @Valid HelloMessage updateMessage(@Valid HelloMessage message) {
message.setMessage("H");
System.out.println("* * * *---==** updateMessage() called **==---* * * *");
return message;
}
Run Code Online (Sandbox Code Playgroud)
然后从命令行再次运行它
当然,如果我用有效数据更正返回值
| 归档时间: |
|
| 查看次数: |
394 次 |
| 最近记录: |