I am trying to figure out how to correctly bootstrap Weld (3.0.0) together with Jersey-Server (1.19.3) to establish a Rest Server with Java SE. I am not! talking about a JEE solution. The application server does a good job here. I would like to have it standalone without all the libs I do not need. My problem is that jersey server instantiates all the REST service classes in his own way and not using weld. Therefore @Inject does not work of course. Does IoCInstantiatedComponentProvider help here ?
1 Answers
This is something Hammock provides, but if you want to just leverage the code that's fine. That's the beauty of open source - https://github.com/hammock-project/hammock/tree/master/rest-jersey/src/main/java/ws/ament/hammock/rest/jersey
Specifically, you'll need to instantiate the JerseyServlet
and provide a ServletContextAttribute
with the name jersey.config.servlet.internal.resourceConfig_JerseyServlet
that points to a ResourceConfig
object that either points to your Application
object, or has all of the classes registered within it. We use a CDI extension to scrape these classes. I will say I'm using Jersey 2 not Jersey 1, I doubt this will work with Jersey 1 (which was made for CDI 1.0). Note that you need a servlet container still.
If you want to leverage Hammock, just add these dependencies to your project (I'm using Jetty below, but you can also use embedded tomcat or undertow):
<dependency>
<groupId>ws.ament.hammock</groupId>
<artifactId>rest-jersey</artifactId>
</dependency>
<dependency>
<groupId>ws.ament.hammock</groupId>
<artifactId>bootstrap-weld3</artifactId>
</dependency>
<dependency>
<groupId>ws.ament.hammock</groupId>
<artifactId>web-jetty</artifactId>
</dependency>
And then you just have to provide some annotated rest resources and a beans.xml

- 11,595
- 1
- 36
- 45
-
Thank you for your answer, but I would like to have a smaller "footprint". You said it yourself: a servlet container is still needed. So I am looking for something "smaller". Mentioning that old jersey 1.X cannot deal with CDI2 is also very helpful. – magicroomy May 27 '17 at 11:30
-
Err, I think you might be misreading. Unless you're expecting to use grizzly, which is effectively what I'm describing (embedded tomcat, jetty, undertow are all just embedded JARs, you still need something to handle the actual HTTP requests which jersey doesn't do) – John Ament May 27 '17 at 12:00
-
Hi, my bad... You are right. By using "jersey-server" I thought it would automatically come with a minimal Http server by calling HtpServer server = HttpServerFactory.create(getURI()). Checking the imports and some delivered classes the http server is the one bundled in the jdk. Since its somewhere deep in sun.com.net.xxx it should not be used => I will definitly try hammock. Thanx – magicroomy May 29 '17 at 06:23