>我编写了一个Java应用程序,目的是使用Undertow服务器托管它并嵌入Jersey,以实现各种API请求。应用程序.java类负责托管服务器和产品端点.java使用泽西实现。
我已经实现了 5 个类。我正在使用Eclipse IDE。
这是我的代码片段:-
1. 申请.java
package mainClass;
import static io.undertow.servlet.Servlets.listener;
import static io.undertow.servlet.Servlets.servlet;
import javax.servlet.ServletException;
import org.glassfish.jersey.servlet.ServletContainer;
import org.jboss.weld.environment.servlet.Listener;
import io.undertow.Handlers;
import io.undertow.Undertow;
import io.undertow.server.handlers.PathHandler;
import io.undertow.servlet.Servlets;
import io.undertow.servlet.api.DeploymentInfo;
import io.undertow.servlet.api.DeploymentManager;
public class Application {
private static Undertow server;
public static void main(String[] args) throws ServletException {
startContainer(9090);
}
public static void stopContainer(){
server.stop();
}
public static void startContainer(int port) throws ServletException {
DeploymentInfo servletBuilder = Servlets.deployment();
servletBuilder
.setClassLoader(Application.class.getClassLoader())
.setContextPath("/mainClass")
.setDeploymentName("mainClass.war")
.addListeners(listener(Listener.class))
.addServlets(servlet("jerseyServlet", ServletContainer.class)
.setLoadOnStartup(1)
.addInitParam("javax.ws.rs.Application", JerseyApp.class.getName())
.addMapping("/api/*"));
DeploymentManager manager = Servlets.defaultContainer().addDeployment(servletBuilder);
manager.deploy();
PathHandler path = Handlers.path(Handlers.redirect("/mainClass"))
.addPrefixPath("/mainClass", manager.start());
server =
Undertow
.builder()
.addHttpListener(port, "localhost")
.setHandler(path)
.build();
server.start();
}
}
2. 泽西岛应用程序.java
package mainClass;
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
@ApplicationPath("/api/*")
public class JerseyApp extends ResourceConfig {
public JerseyApp() {
packages(true, "mainClass");
}
}
3. 对象映射器工厂.java
package mainClass;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
public class ObjectMapperFactory {
private static final ObjectMapper OBJECT_MAPPER;
static {
OBJECT_MAPPER = new ObjectMapper();
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
OBJECT_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
private ObjectMapperFactory() {
}
public static ObjectMapper get() {
return OBJECT_MAPPER;
}
}
4. 对象映射解析器.java
package mainClass;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import com.fasterxml.jackson.databind.ObjectMapper;
@Provider
public class ObjectMapperResolver implements ContextResolver<ObjectMapper> {
@Override
public ObjectMapper getContext(Class<?> type) {
return ObjectMapperFactory.get();
}
}
5. 产品端点.java
package mainClass;
import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@Path("/v1")
@ApplicationScoped
public class ProductEndpoint {
@GET
@Path("/hello")
public String PrintHello(){
return "Hello";
}
@GET
@Path("/hai")
public String PrintHai(){
return "Hai";
}
@GET
@Path("/selfhost")
public String PrintSelfHost(){
return "Self Host";
}
}
但是,在运行 Java 代码时,会抛出如下所示的错误,并且我无法托管服务器。
Feb 10, 2020 5:22:17 PM org.jboss.weld.environment.servlet.Listener contextInitialized
INFO: WELD-ENV-001007: Initialize Weld using ServletContextListener
Feb 10, 2020 5:22:17 PM org.jboss.weld.bootstrap.WeldStartup <clinit>
INFO: WELD-000900: 2.2.14 (Final)
Feb 10, 2020 5:22:17 PM org.jboss.weld.environment.servlet.WeldServletLifecycle initialize
INFO: WELD-ENV-000028: Weld initialization skipped - no bean archive found
Exception in thread "main" java.lang.RuntimeException: java.lang.IllegalStateException: Singleton not set for STATIC_INSTANCE => []
at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:224)
at mainClass.Application.startContainer(Application.java:44)
at mainClass.Application.main(Application.java:23)
Caused by: java.lang.IllegalStateException: Singleton not set for STATIC_INSTANCE => []
at org.jboss.weld.bootstrap.api.helpers.RegistrySingletonProvider$RegistrySingleton.get(RegistrySingletonProvider.java:28)
at org.jboss.weld.Container.instance(Container.java:55)
at org.jboss.weld.SimpleCDI.<init>(SimpleCDI.java:77)
at org.jboss.weld.environment.WeldProvider$EnvironmentCDI.<init>(WeldProvider.java:45)
at org.jboss.weld.environment.WeldProvider.getCDI(WeldProvider.java:61)
at javax.enterprise.inject.spi.CDI.current(CDI.java:60)
at org.jboss.weld.servlet.WeldInitialListener.contextInitialized(WeldInitialListener.java:97)
at org.jboss.weld.servlet.api.helpers.ForwardingServletListener.contextInitialized(ForwardingServletListener.java:34)
at org.jboss.weld.environment.servlet.Listener.contextInitialized(Listener.java:57)
at io.undertow.servlet.core.ApplicationListeners.contextInitialized(ApplicationListeners.java:173)
at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:195)
... 2 more
有人可以帮助我修复此错误吗?有没有更简单的方法将泽西岛集成到暗流服务器上?
注意:我在不使用任何框架的情况下创建了应用程序。我添加了所需库的JAR文件。
我也使用Undertow fine和Jersey jaxrs-ri fine,但没有org.jboss.weld.environment.servlet.Listener
。 对于依赖注入,我只使用jersey内置支持ServiceBinder
(应用程序的子类(以及org.glassfish.hk2.utilities.binding.AbstractBinder
。 请参阅 https://docs.jboss.org/weld/reference/1.0.0/en-US/html/environments.html