Java 码头嵌入式,泽西岛2,焊接
我使用的是Jetty 9.1和Jersey 2.5.1。Jersey内置了对Jetty的支持,因此我启动服务器的方式如下:Java 码头嵌入式,泽西岛2,焊接,java,jetty,jersey-2.0,weld,Java,Jetty,Jersey 2.0,Weld,我使用的是Jetty 9.1和Jersey 2.5.1。Jersey内置了对Jetty的支持,因此我启动服务器的方式如下: public static void main(String[] args) { URI baseUri = UriBuilder.fromUri("http://localhost/").port(8080).build(); ResourceConfig config = ResourceConfig.forApplicationClass(MyApp
public static void main(String[] args) {
URI baseUri = UriBuilder.fromUri("http://localhost/").port(8080).build();
ResourceConfig config = ResourceConfig.forApplicationClass(MyApplication.class);
Server server = JettyHttpContainerFactory.createServer(baseUri, config);
}
MyApplication只需调用this.packages(…)
来查找我的RESTAPI类
但是,RESTAPI类包含一个@Inject
注释字段,该字段应通过WELD注入。显然,WELD未启动(CDI支持未启用),更奇怪的是,看起来HK2(由Jersey 2使用)正在尝试执行注入
(我有一个org.glassfish.hk2.api.unsatifiededpendencyException
在命中REST端点时)
如何正确设置焊接(最好是编程设置)?我使用了焊接SE:
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
然后简单地
Weld weld = new Weld();
try {
WeldContainer container = weld.initialize();
URI baseUri = UriBuilder.fromUri("http://localhost/").port(8080).build();
ResourceConfig config = ResourceConfig.forApplicationClass(MyApplication.class);
Server server = JettyHttpContainerFactory.createServer(baseUri, config);
server.join();
} catch (Exception e) {
e.printStackTrace();
} finally {
weld.shutdown();
}
请注意,HK2将处理REST类,因此我必须编写一个活页夹,以便在这些类中进行注入 确保您使用的是Weld 2.2+,因为它刚刚修复了Jetty 9.1+的一些CDI集成错误。