[英]How to add a Jersey REST webservice into embedded tomcat?
Basically I want to run some Rest classes in Tomcat 8 embedded. 基本上我想在Tomcat 8嵌入式中运行一些Rest类。 I am unsure how to add them to the tomcat embedded instance I am creating. 我不确定如何将它们添加到我正在创建的tomcat嵌入式实例中。 So this is what I do. 所以这就是我的工作。 Here is just that Jersey class: 这只是泽西岛的类:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import static javax.ws.rs.core.MediaType.*;
@Path("register")
public class RegisterRestAPI {
private MerchantRegistrationService merchantRegistrationService;
public RegisterRestAPI(MerchantRegistrationService merchantRegistrationService) {
this.merchantRegistrationService = merchantRegistrationService;
}
@GET
@Produces(TEXT_PLAIN)
public String register() {
return "Hello!!!!";
}
}
And here is the class where I create Tomcat: 这是我创建Tomcat的类:
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.init.JerseyServletContainerInitializer;
import javax.servlet.ServletException;
import java.io.File;
public class TomcatServer {
private MerchantRegistrationService merchantRegistrationService;
public TomcatServer(MerchantRegistrationService merchantRegistrationService)
{
this.merchantRegistrationService = merchantRegistrationService;
}
public void start() throws ServletException, LifecycleException {
String webappDirLocation = "restui/src/main/webapp/";
Tomcat tomcat = new Tomcat();
String webPort = System.getenv("PORT");
if(webPort == null || webPort.isEmpty()) {
webPort = "8080";
}
tomcat.setPort(Integer.valueOf(webPort));
Context context = tomcat.addWebapp("/", new File(webappDirLocation).getAbsolutePath());
tomcat.addServlet(context,"jersey-container-servlet",resourceConfig());
context.addServletMapping("/register", "registration rest");
tomcat.start();
tomcat.getServer().await();
}
private ServletContainer resourceConfig() {
return new ServletContainer(new ResourceConfig().register(new
RegisterRestAPI(merchantRegistrationService)));
}
}
So as you see that is the part with the question marks is giving me trouble to create. 因此,正如您所看到的那样,带有问号的部分让我无法创建。 Also, just one lats question, this is the way I should add those classes to Run on server right? 另外,只有一个问题,这是我应该将这些类添加到服务器上运行的方式吗?
Update I added the line suggested by Michal Gajdos but at startup I get: 更新我添加了Michal Gajdos建议的行,但在启动时我得到:
Exception in thread "main" java.lang.IllegalArgumentException: Servlet mapping specifies an unknown servlet name registration rest at org.apache.catalina.core.StandardContext.addServletMapping(StandardContext.java:3160) at org.apache.catalina.core.StandardContext.addServletMapping(StandardContext.java:3139) at com.crypto.restui.TomcatServer.start(TomcatServer.java:44) at com.crypto.assembler.Boot.main(Boot.java:22) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) 线程“main”中的异常java.lang.IllegalArgumentException:Servlet映射在org.apache.catalina.core.StandardContext的org.apache.catalina.core.StandardContext.addServletMapping(StandardContext.java:3160)中指定了未知的servlet名称注册休息。 .addServletMapping(StandardContext.java:3139)位于com.crypto.restui.TomcatServer.start(TomcatServer.java:44)的com.crypto.assembler.Boot.main(Boot.java:22)at sun.reflect.NativeMethodAccessorImpl。在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)的sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)的java.lang.reflect.Method.invoke上的invoke0(Native Method)(Method.java: 606)at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
How should I call the servlet? 我该如何调用servlet?
ServletContainer extends HttpServlet
and can be passed to the underlying servlet container, simply create new instance: ServletContainer扩展了HttpServlet
,可以传递给底层的servlet容器,只需创建新的实例:
new ServletContainer(new ResourceConfig(RegisterRestAPI.class));
You can also define servlet in web.xml
and pass reference to this descriptor to Tomcat - similarly as done for Jetty here . 您还可以在web.xml
定义servlet并将对此描述符的引用传递给Tomcat - 与此处的Jetty类似。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.