我想在我的应用程序中使用嵌入式tomcat服务器。我正在尝试使用 Jersey JAX-RS 创建RESTful服务,当调用它时会返回银行名称,特定银行名称的IFSC代码等数据。
当我在服务器上部署时(在Eclipse中,右键单击项目文件夹 - >运行为 - >在服务器上运行,然后选择tomcat服务器),它工作正常和URL正确映射,我正在获取数据。但是当我使用嵌入式tomcat服务器时,它无法正常工作并且显示无法加载资源。
我在某地读过嵌入式tomcat需要主类,所以我按如下方式实现了主类:参考:http://blog.sortedset.com/embedded-tomcat-jersey/
public class Main {
public static void main(String[] args) throws Exception, LifecycleException {
new Main().start();
}
public void start() throws ServletException, LifecycleException,
MalformedURLException {
// Define a folder to hold web application contents.
String webappDirLocation = "src/main/webapp/";
Tomcat tomcat = new Tomcat();
// Define port number for the web application
String webPort = System.getenv("PORT");
if (webPort == null || webPort.isEmpty()) {
webPort = "8080";
}
// Bind the port to Tomcat server
tomcat.setPort(Integer.valueOf(webPort));
// Define a web application context.
Context context = tomcat.addWebapp("/tomcatembedded", new File(
webappDirLocation).getAbsolutePath());
// Define and bind web.xml file location.
File configFile = new File(webappDirLocation + "WEB-INF/web.xml");
context.setConfigFile(configFile.toURI().toURL());
tomcat.start();
tomcat.getServer().await();
}
}
现在右键单击项目 - >作为Java应用程序运行, 该网址无法加载数据: localhost:8080 / tomcatembedded / webapi / banksList
我缺少的是,网址错误还是我错过了一些配置?
我的目录结构是: Eclipse Directory structure
我的web.xml是:
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>sran.api.ifscLookup.resources</param-value>
</init-param>
<!--init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>