JavaJersey 2:独立?
是否可以在java应用程序中独立使用Jersey REST服务? 我发现的每个示例都在webcontainer(web.xml)的上下文中 谢谢你的帮助 只需遵循以下步骤。它使用Grizzly服务器创建一个独立的服务器。使用Maven,您可以使用以下archetype命令轻松创建它JavaJersey 2:独立?,java,jersey,Java,Jersey,是否可以在java应用程序中独立使用Jersey REST服务? 我发现的每个示例都在webcontainer(web.xml)的上下文中 谢谢你的帮助 只需遵循以下步骤。它使用Grizzly服务器创建一个独立的服务器。使用Maven,您可以使用以下archetype命令轻松创建它 mvn原型:generate-DarchetypeArtifactId=jersey-quickstart-grizzly2\ -DarchetypeGroupId=org.glassfish.jersey.arch
mvn原型:generate-DarchetypeArtifactId=jersey-quickstart-grizzly2\
-DarchetypeGroupId=org.glassfish.jersey.archetypes-DinteractiveMode=false\
-DgroupId=com.example-DartifactId=simple service-Dpackage=com.example\
-DarchetypeVersion=2.27
最后一个参数-DarchetypeVersion
可以替换为所需的任何2.x版本。目前,最新版本为2.27
运行此命令后,将生成一个从命令行运行的主类
另一种方法是使用(很多人都在使用),您可以签出并签出
更新 如果您没有使用Maven,下面是使用原型的所有jar。大多数都包含在该列表中。只需找出那些不在捆绑包中的,然后搜索它们 我认为没有包含在RI包DL中的是
- 网格框架
- grizzly http
- grizzly http服务器
- jersey-container-grizzly2-http
package com.example;
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!";
}
}
com.example.Main
package com.example;
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.example package
final ResourceConfig rc = new ResourceConfig().packages("com.example");
// 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();
}
}
(test)com.example.MyResourceTest
package com.example;
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);
}
}
我要和灰熊一起试试。据我所知,它不在最新的jersey捆绑包(jaxrs-ri-2.27.zip)中。我刚刚通过谷歌搜索找到了JAR,但我想我在那里遇到了一些版本冲突。有给灰熊的官方包裹吗?我不使用maven,所以这有点手工。我更新了我的答案,以显示原型引入的所有JAR,并发布了所有生成的类。