0

I have the following structure ina project I am using to learn JavaEE7:

Bean:

 @Stateless
    public class MyBean implements MyInterface{
            public String lookup(@NotNull String text){
                 return "found3";
            }
    }

Interface:

public interface MyInterface {
    public String lookup(@NotNull String text);
}

and a second bean:

public class HelloWorld {
    @Inject
    private MyInterface bean;

   public String getMessage() {
      return bean.lookup(null);
   }
}

My server is a WildFly 8.2.

I would like to have the lookup method validated when calling it, so I annotated the parameter so it doesn't accept null.

The problem is that this code runs OK when I call the HelloWorld.getMessages() (I get the return value "found3".

If I copy the validation form MyInterface to MyBean, I get the validaiton exception as I wanted.

Is it possible to declare the validation in the interface? How do I do that?

JSBach
  • 4,679
  • 8
  • 51
  • 98
  • Ok, that would be the answer. Could you please convert it to an answer so I can mark it? – JSBach May 15 '15 at 11:28
  • Actually, I think there is more to it. According to the Bean Validation spec, the right way is to annotate the interface. See also http://beanvalidation.org/1.1/spec/#constraintdeclarationvalidationprocess-methodlevelconstraints-inheritance. Due to the Liskov Substitution Principle, I would even expect that placing a constraint on the parameter in the MyBean method throws a configuration exception. Inheritance of annotations as suggested in http://stackoverflow.com/a/30258428/115835 does not play a role here. – Hardy May 18 '15 at 09:31
  • Weird... That means my code above should work... If I change the interface to a abstract class, it works, so the "infrastructure" is working correctly. Inspecting the bean I am able to check that the annotation is not there. Any ideas on what I could do to solve this? – JSBach May 18 '15 at 09:37
  • btw: I get no exception when annotating the implementing class. – JSBach May 18 '15 at 09:38
  • 1
    No, it seems odd to me. Hard to tell what's wrong w/o more context. Maybe you could extract a test case and report it here - https://hibernate.atlassian.net/projects/HV – Hardy May 18 '15 at 09:52
  • btw, I already posted what I thought it would be a defect and you answered it there, clarifying my question. Thanks! – JSBach May 18 '15 at 11:51

0 Answers0