如何将Jersey REST Web服务添加到嵌入式tomcat中? [英] How to add a Jersey REST webservice into embedded tomcat?

查看:93
本文介绍了如何将Jersey REST Web服务添加到嵌入式tomcat中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想在嵌入式Tomcat 8中运行一些Rest类. 我不确定如何将它们添加到我正在创建的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!!!!";
    }
}

这是我创建Tomcat的类:

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?

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

Update I added the line suggested by Michal Gajdos but at startup I get:

线程"main"中的异常java.lang.IllegalArgumentException:Servlet 映射指定未知的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)处 com.crypto.assembler.Boot.main(Boot.java:22)位于 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:606)在 com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

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)

我应该如何调用servlet?

How should I call the servlet?

推荐答案

ServletContainer 扩展HttpServlet并可以传递到基础servlet容器,只需创建新实例即可:

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

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

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

You can also define servlet in web.xml and pass reference to this descriptor to Tomcat - similarly as done for Jetty here.

这篇关于如何将Jersey REST Web服务添加到嵌入式tomcat中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆