如何在不使用tomcat的情况下运行jersey-server webservice服务器

这是我第一次处理网络服务。 简单地说,我需要从泽西网络服务客户端(在javascript中实现的网页内)发送一个post请求到我的maven模块之一的jersey服务。

正如我所说,我已经在我的一个maven模块中创建了jersey-server,我想以某种方式运行它(我不知道如何运行Web服务程序。)在启动我的实现的客户端之前。 通过在网上搜索,我看到了很多例子,但他们都使用了tomcat。 所以我的第一个问题是,我是否需要使用tomcat(或类似的东西)才能运行Web服务? 其次,下面我分享了我的泽西服务器模块。 我怎么能开始运行呢?

package com.exampleProject.rest; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.util.List; @Path("/test") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public class SiderRecommender { @POST @Path("/functiontest") public List sampleFunction() { // return something here. I removed it for simplicity. } } 

您不必在已安装的Web服务器中运行Jersey应用程序。 您可以在嵌入式服务器中运行它,这意味着服务器使用常规main方法以独立模式运行。

如果您正在使用Maven,并且熟悉创建Maven原型,则可以使用jersey-quickstart-grizzly2原型

  • 从命令行
  • 来自Eclipse (除了使用jersey-quickstart-grizzly2
  • 来自Netbeans (见答案的底部。也使用jersey-quickstart-grizzly2 )。

这是您使用原型项目免费获得的所有内容。

在此处输入图像描述

Main.java

 package com.underdog.jersey.grizzly; import org.glassfish.grizzly.http.server.HttpServer; import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; import org.glassfish.jersey.server.ResourceConfig; import java.io.IOException; import java.net.URI; /** * Main class. * */ public class Main { // Base URI the Grizzly HTTP server will listen on public static final String BASE_URI = "http://localhost:8080/myapp/"; /** * Starts Grizzly HTTP server exposing JAX-RS resources defined in this application. * @return Grizzly HTTP server. */ public static HttpServer startServer() { // create a resource config that scans for JAX-RS resources and providers // in com.underdog.jersey.grizzly package final ResourceConfig rc = new ResourceConfig().packages("com.underdog.jersey.grizzly"); // create and start a new instance of grizzly http server // exposing the Jersey application at BASE_URI return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc); } /** * Main method. * @param args * @throws IOException */ public static void main(String[] args) throws IOException { final HttpServer server = startServer(); System.out.println(String.format("Jersey app started with WADL available at " + "%sapplication.wadl\nHit enter to stop it...", BASE_URI)); System.in.read(); server.stop(); } } 

MyResource.java

 package com.underdog.jersey.grizzly; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; /** * Root resource (exposed at "myresource" path) */ @Path("myresource") public class MyResource { /** * Method handling HTTP GET requests. The returned object will be sent * to the client as "text/plain" media type. * * @return String that will be returned as a text/plain response. */ @GET @Produces(MediaType.TEXT_PLAIN) public String getIt() { return "Got it!"; } } 

MyResourceTest.java

 package com.underdog.jersey.grizzly; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.WebTarget; import org.glassfish.grizzly.http.server.HttpServer; import org.junit.After; import org.junit.Before; import org.junit.Test; import static org.junit.Assert.assertEquals; public class MyResourceTest { private HttpServer server; private WebTarget target; @Before public void setUp() throws Exception { // start the server server = Main.startServer(); // create the client Client c = ClientBuilder.newClient(); // uncomment the following line if you want to enable // support for JSON in the client (you also have to uncomment // dependency on jersey-media-json module in pom.xml and Main.startServer()) // -- // c.configuration().enable(new org.glassfish.jersey.media.json.JsonJaxbFeature()); target = c.target(Main.BASE_URI); } @After public void tearDown() throws Exception { server.stop(); } /** * Test to see that the message "Got it!" is sent in the response. */ @Test public void testGetIt() { String responseMsg = target.path("myresource").request().get(String.class); assertEquals("Got it!", responseMsg); } } 

pom.xml – 我自己添加了jersey-media-json-jacksonmaven-assembly-plugin ,这样你就可以创建一个可运行的jar文件。

  4.0.0 com.underdog jersey-grizzly jar 1.0-SNAPSHOT jersey-grizzly    org.glassfish.jersey jersey-bom ${jersey.version} pom import      org.glassfish.jersey.containers jersey-container-grizzly2-http   org.glassfish.jersey.media jersey-media-json-jackson   junit junit 4.9 test    ${project.artifactId}   maven-assembly-plugin 2.5.3   jar-with-dependencies    com.underdog.jersey.grizzly.Main      create-archive package  single      org.apache.maven.plugins maven-compiler-plugin 2.5.1 true  1.7 1.7    org.codehaus.mojo exec-maven-plugin 1.2.1    java     com.underdog.jersey.grizzly.Main      2.17 UTF-8   

综上所述,您可以从命令行cd到项目并执行

  1. mvn clean package
  2. java -jar target/jersey-grizzly-jar-with-dependencies.jar

并且应用程序将启动。

您可以从http://localhost:8080/myapp/myresource访问它

而已。 请注意,以上是一个普通的jar项目。 因此,如果您无法遵循如何创建原型,您几乎可以将上述所有内容复制到jar项目中。

也可以看看:

  • Jersey开始使用Maven进行更多解释。

我无法评论第一个答案。 所以我在这里添加评论。 如果选择使用原型生成项目,则命令行应为

 mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 -DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false -DgroupId=com.underdog.jersey.grizzly -DartifactId=simple-service -Dpackage=com.underdog.jersey.grizzly -DarchetypeVersion=2.23.1 

而不是链接中的命令行。 这将与peeskillet提供的pom一起使用

由于您使用Maven,因此您需要配置在Tomcat上运行的属性。 运行配置

右键单击Project – > Run as-> Run Configuration选择Maven Tomcat->将目标更改为’tomcat:run’