Java 是否可以设置下拖以服务于弹簧休息端点?
我的主要目标是在没有任何Java 是否可以设置下拖以服务于弹簧休息端点?,java,spring,rest,undertow,Java,Spring,Rest,Undertow,我的主要目标是在没有任何web.xml和Spring Boot的情况下在我的应用程序中设置嵌入式。Undertow看起来离servlet容器足够近,可以满足我的需求,同时具有超性能和精益。至于其他的微框架,我也看了,但我先尝试一下Undertow,因为它的文档看起来更好 所以Undertow听起来不错,但我遇到的所有文档和教程都在/上返回“Hello World”后停止。也许我能找到的最好方法是所有端点都是硬编码的,例如: public static final RoutingHandler R
web.xml
和Spring Boot的情况下在我的应用程序中设置嵌入式。Undertow看起来离servlet容器足够近,可以满足我的需求,同时具有超性能和精益。至于其他的微框架,我也看了,但我先尝试一下Undertow,因为它的文档看起来更好
所以Undertow听起来不错,但我遇到的所有文档和教程都在/
上返回“Hello World”后停止。也许我能找到的最好方法是所有端点都是硬编码的,例如:
public static final RoutingHandler ROUTES=new RoutingHandler()
.get(“/users”,定时(“listUsers”,UserRoutes::listUsers))
我找不到任何东西显示如何或者是否有可能将SpringMVC/REST控制器注释与基本的Undertow结构连接起来
我已经有了一个应用程序,它在Spring注释中定义了一组端点
关于如何将二者结合起来,我对弹簧和下拖的了解中缺少了一大块,但我可以从中看出,弹簧提供了一种在后备箱中使用下拖的方法。我只是不需要弹簧靴。我真的不热衷于挖掘,看看它有多重要,因为在我的情况下,它可能不会被复制。这是在引导中实现它的方法:
@Bean
公共底层嵌入的ServletContainerFactory嵌入的ServletContainerFactory(){
UndertowEmbeddedServletContainerFactory工厂=
新的UndertowEmbeddedServletContainerFactory();
factory.addbuildercustomizer(新的buildercustomizer()下){
@凌驾
公共空间自定义(io.undertow.undertow.Builder){
builder.addHttpListener(8080,“0.0.0.0”);
}
});
返回工厂;
}
我的猜测是,我必须以编程的方式获取带注释的Spring REST控制器,并为每个控制器创建所需的拖曳资源
这似乎是下层邮件列表是无法搜索的 我不得不咬紧牙关,仔细阅读Spring的源代码,看看Pivotal是如何将这一切联系在一起的 在提取了相关的部分之后,我重构了我得到的,并将其归结为要点。第一个是集成测试类
private static Undertow server;
@BeforeAll
public static void startServer() throws ServletException {
server = ServletUtils.buildUndertowServer(
8080,
"localhost",
"",
SpringServletContainerInitializer.class,
Collections.singleton(
MySpringServletInitializer.class),
MyTests.class.getClassLoader());
server.start();
}
@AfterAll
public static void stopServer() {
try {
if (server != null) {
server.stop();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testIsAvailable() {
Response response = get("/mystuff/isAvailable");
response.then().statusCode(200);
ResponseBody body = response.getBody();
assertThat("Body", body.asString(), is(equalTo("ok")));
}
我在一个实用课上做过海底管道。我将其与Spring完全分离,这就是为什么我将javax.servlet.ServletContainerInitializer的Spring实现作为参数发送
import io.undertow.servlet.api.ServletContainerInitializerInfo;
import javax.servlet.ServletContainerInitializer;
public static Undertow buildUndertowServer(
int port,
String address,
String contextPath,
Class<? extends ServletContainerInitializer>
servletContainerInitializerClass,
Set<Class<?>> initializers,
ClassLoader classLoader
) throws ServletException {
ServletContainerInitializerInfo servletContainerInitializerInfo =
new ServletContainerInitializerInfo(
servletContainerInitializerClass,
initializers);
DeploymentInfo deployment = Servlets.deployment();
deployment.addServletContainerInitializer(
servletContainerInitializerInfo);
deployment.setClassLoader(classLoader);
deployment.setContextPath(contextPath);
deployment.setDisplayName("adam");
deployment.setDeploymentName("int-test");
deployment.setServletStackTraces(ServletStackTraces.ALL);
DeploymentManager manager =
Servlets.newContainer().addDeployment(deployment);
manager.deploy();
Undertow.Builder builder = Undertow.builder();
builder.addHttpListener(port, address);
HttpHandler httpHandler = manager.start();
httpHandler = Handlers.path().addPrefixPath(contextPath, httpHandler);
builder.setHandler(httpHandler);
return builder.build();
}
此Spring servlet初始值设定项将通过实现AnnotationConfigWebApplicationContext
(在getRootConfigClasses
方法中)调用Spring上下文初始化:
以这种方式启动整个SpringREST服务器大约需要1秒。
重新开始测试,一切都很顺利 我创建了一个使用Spring和Undertow构建的示例项目。它还与JSP和OpenAPI3.0集成。您可以在这里查看:您的代码中可能存在的键入错误:“server=ServletUtils.buildUndertowServer”应该是“server=Undertow.buildUndertowServer”?非常有用的代码,但在我的情况下,我必须直接将MySpringWebApplicationContext提供给ServletContainerInitializerInfo,而不使用MySpringServletInitializer。不知道原因,我只是尝试了:-)否则我在MySpringWebApplicationContext中的方法不会被调用(你没有)。
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
public class MySpringServletInitializer
extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {
MySpringWebApplicationContext.class
};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
public void onStartup(ServletContext servletContext)
throws ServletException {
logger.info("starting up");
super.onStartup(servletContext);
}
}
@PropertySource("file:target/application-int.properties")
@Configuration
@ComponentScan(basePackages = { "org.adam.rest" })
@EnableWebMvc
public class MySpringWebApplicationContext
extends AnnotationConfigWebApplicationContext {
}