如何将Jersey REST Web服务添加到嵌入式tomcat中?

时间:2014-01-16 19:50:57

标签: java rest tomcat jersey

基本上我想在Tomcat 8中运行一些Rest类。 我不确定如何将它们添加到我正在创建的tomcat嵌入式实例中。 所以这就是我的工作。 这就是Jersey课程:

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!!!!";
    }
}

这是我创建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)));
    }
}

因此,正如您所看到的那样,带有问号的部分让我无法创建。 另外,只有一个问题,这是我应该将这些类添加到服务器上运行的方式吗?

更新 我添加了Michal Gajdos建议的那条线,但在启动时我得到了:

  

线程“main”中的异常java.lang.IllegalArgumentException:Servlet   mapping指定一个未知的servlet名称注册休息   org.apache.catalina.core.StandardContext.addServletMapping(StandardContext.java:3160)     在   org.apache.catalina.core.StandardContext.addServletMapping(StandardContext.java:3139)     在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)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:606)at   com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

我该如何调用servlet?

1 个答案:

答案 0 :(得分:3)

ServletContainer扩展HttpServlet并可以传递给底层servlet容器,只需创建新实例:

new ServletContainer(new ResourceConfig(RegisterRestAPI.class));

您还可以在web.xml中定义servlet并将对此描述符的引用传递给Tomcat - 与Jetty here类似。