java rest tomcat,如何将Jersey REST Web服务添加到嵌入式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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值