简体   繁体   中英

How to add a Jersey REST webservice into embedded tomcat?

Basically I want to run some Rest classes in Tomcat 8 embedded. I am unsure how to add them to the tomcat embedded instance I am creating. 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:

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:

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)

How should I call the servlet?

ServletContainer extends HttpServlet and can be passed to the underlying servlet container, simply create new instance:

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM