![](/img/trans.png)
[英]How to deploy java spring restful service on AWS based on tomcat server with mysql database?
[英]How to add a Spring based RESTful service as Servlet to Tomcat
我正在开发使用类似模块方法的应用程序。 几个模块在运行时加载。 一个模块是Tomcat Web服务器,另一个模块是使用Spring的RESTful API的实现。 我想在运行时将整个基于Spring的RESTful API作为servlet添加到Tomcat。 这可能吗?
我已经阅读了有关Spring Boot的文章,它看起来很有前途,但是我只找到有关创建可以部署到Tomcat的WAR文件的信息。 但是,这是不可能的,因为RESTful API必须是一个模块。 谢谢您的帮助!
Web服务器模块运行以下代码来创建和配置Tomcat Web服务器。
Tomcat tomcat = new Tomcat();
// this defines Tomcat's working directory
File base = new File(System.getProperty("java.io.tmpdir"));
Context rootContext = tomcat.addContext("", base.getAbsolutePath());
// more configuration ...
REST API模块创建和配置Spring应用程序上下文,如下所示:
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(SomeAnnotatedClass.class);
context.register(AnotherAnnotatedClass.class);
// more configuration ...
context.refresh();
// this is the servlet that we need to add to Tomcat
DispatcherServlet servlet = new DispatcherServlet(context);
现在,我们将所有内容放在一起,并将调度程序servlet添加到Tomcat。
Context rootContext = getTomcatsRootContextFromSomewhere();
Servlet dispatcherServlet = getDispatcherServletFromSomewhere();
// add the dispatcher servlet to the root context
Tomcat.addServlet(rootContext, "mySpringService", dispatcherServlet);
// add a mapping to the dispatcher servlet
rootContext.addServletMapping("/api/*", "mySpringService");
我认为这不是最佳解决方案,因为我仍然是Spring / Tomcat新手。 如果您有一些建议,我将不胜感激。
我希望我能够为遇到这个特殊问题的人提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.