0

I'm trying to use constructor validation as described here:

https://docs.oracle.com/javaee/7/tutorial/bean-validation003.htm

I have a simple producer field that provides a string...

public class GreetingProducer {

    @Produces @Named("greeting")
    private String greeting = "Ron";

}

... which is injected into a servlet:

@WebServlet(urlPatterns = "/test")
@SuppressWarnings("serial")
public class TestServlet extends HttpServlet {

    private final String greeting;

    @Inject
    public TestServlet(@Named("greeting") @Size(min = 4) String greeting) {
        this.greeting = greeting;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().println("Hello " + greeting);
    }

}

Deployment on Wildfly 9 doesn't yield any errors and calling the servlet prints Hello Ron, although the deployment should fail. I also tried different flavours of constraint declaration but nothing worked.

Any ideas why the validation doesn't kick in?

assylias
  • 321,522
  • 82
  • 660
  • 783
tamm0r
  • 179
  • 1
  • 2
  • 12
  • 1
    On what basis do you expect a deployment error? In fact, on what basis do you expect bean validation to be applied to servlets at all? I'm not necessarily saying it *shouldn't* be applied, but it seems targeted at different parts of Java EE than you're trying to use it with. – John Bollinger Feb 09 '16 at 23:00
  • @JohnBollinger According to the [Java EE 7 Tutorial](https://docs.oracle.com/javaee/7/tutorial/cdi-basic004.htm#GJFZI) _a top-level Java class is a (CDI) managed bean if it is defined to be a managed bean by any other Java EE technology specification_. The [BV 1.1 spec](http://beanvalidation.org/1.1/spec/#integration-cdi) mandates that _in a Java EE container, a Bean Validation provider must integrate with CDI_ and _Bean Validation requires that CDI beans support constructor and method validation_. I'd say it has to work ;-) – tamm0r Feb 10 '16 at 00:00
  • I'm not sure that a servlet would be considered to be a "managed bean". I don't think the likes of `@PostConstruct`, etc will work on servlets either. – Steve C Feb 10 '16 at 04:59

0 Answers0