gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:08:34 25 4
gpt4 key购买 nike

基本上我想在嵌入式 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)));
}
}

如您所见,带问号的部分让我难以创作。另外,只有一个 lats 问题,这是我应该将这些类添加到在服务器上运行的方式吗?

更新我添加了 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)

我该如何调用servlet?

最佳答案

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

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

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

关于java - 如何将 Jersey REST web 服务添加到嵌入式 tomcat 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21171472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com