How to implement JAX-RS RESTful service in JSF framework
Solution 1:
This concern is unnecessary. You can safely have multiple servlets in a single web application, as long as their URL patterns do not clash with each other. Usually, if that were the case, a bit sane servlet container would already throw an exception during webapp's startup. In your case, you've registered the JSF servlet on /test/*
(which is a strange, by the way, you usually use *.xhtml
for that), and you've registered the JAX-RS servlet on /api/*
. So you just have to call them using URLs matching those URL patterns.
And, to clear out a conceptual misunderstanding, you don't and can't "implement REST in JSF" at all. They are completely independent from each other. They can just easily run next each other in the same web application in all peace without knowing about each other. The only thing which they might share is the service layer or 'shared' (CDI) managed beans. But that's usually it. The design of the service layer is in turn independent from who's using it.
This specific problem is not related to JSF nor JAX-RS. It's just basic servlets. It might be as well worth the effort to take a step back to the basics and spend a bit time to learn more about the building stone of basically every Java EE web application.
Solution 2:
You cannot directly have both in the same java class or layer. I implemented it like below
|---> JSF Managed Beans(Inject logic here)
Database --> Daos --> Logic --|
|---> JAX-RS (Inject logic here)