Java Jersey 2:独立?
是否可以在 Java 应用程序中独立使用 Jersey REST 服务?我找到的每个示例都在 web 容器 (web.xml) 的上下文中.
感谢您的帮助!
解决方案只要按照
我认为不包含在 RI 捆绑包 DL 中的是
- grizzly 框架
- grizzly-http
- grizzly-http-服务器
- jersey-container-grizzly2-http
这里是从原型生成的所有类.
com.example.MyResource
包 com.example;导入 javax.ws.rs.GET;导入 javax.ws.rs.Path;导入 javax.ws.rs.Produces;导入 javax.ws.rs.core.MediaType;/*** 根资源(暴露在myresource"路径)*/@Path("我的资源")公共类 MyResource {/*** 处理 HTTP GET 请求的方法.返回的对象将被发送* 以text/plain"媒体类型发送给客户端.** @return 将作为文本/纯文本响应返回的字符串.*/@得到@Produces(MediaType.TEXT_PLAIN)公共字符串 getIt() {返回知道了!";}}
com.example.Main
包 com.example;导入 org.glassfish.grizzly.http.server.HttpServer;导入 org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;导入 org.glassfish.jersey.server.ResourceConfig;导入 java.io.IOException;导入 java.net.URI;/*** 主要课程.**/公共类主要{//Grizzly HTTP 服务器将侦听的基础 URI公共静态最终字符串 BASE_URI = "http://localhost:8080/myapp/";/*** 启动 Grizzly HTTP 服务器,公开此应用程序中定义的 JAX-RS 资源.* @return 灰熊 HTTP 服务器.*/公共静态 HttpServer startServer() {//创建一个扫描 JAX-RS 资源和提供者的资源配置//在 com.example 包中final ResourceConfig rc = new ResourceConfig().packages("com.example");//创建并启动一个新的 grizzly http 服务器实例//在 BASE_URI 上暴露 Jersey 应用程序返回 GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);}/*** 主要方法.* @param 参数* @throws IOException*/公共静态 void main(String[] args) 抛出 IOException {最终的 HttpServer 服务器 = startServer();System.out.println(String.format("Jersey 应用程序以 WADL 启动,可在 "+ "%sapplication.wadl
按回车键停止它...", BASE_URI));System.in.read();server.stop();}}
(测试)com.example.MyResourceTest
包 com.example;导入 javax.ws.rs.client.Client;导入 javax.ws.rs.client.ClientBuilder;导入 javax.ws.rs.client.WebTarget;导入 org.glassfish.grizzly.http.server.HttpServer;导入 org.junit.After;导入 org.junit.Before;导入 org.junit.Test;导入静态 org.junit.Assert.assertEquals;公共类 MyResourceTest {私有 HttpServer 服务器;私人 WebTarget 目标;@前公共 void setUp() 抛出异常 {//启动服务器服务器 = Main.startServer();//创建客户端客户端 c = ClientBuilder.newClient();//如果要启用,请取消注释以下行//在客户端支持 JSON(您还必须取消注释//依赖 pom.xml 和 Main.startServer() 中的 jersey-media-json 模块//--//c.configuration().enable(new org.glassfish.jersey.media.json.JsonJaxbFeature());目标 = c.target(Main.BASE_URI);}@后public void tearDown() 抛出异常 {server.stop();}/*** 测试以查看消息知道了!"在响应中发送.*/@测试公共无效 testGetIt() {String responseMsg = target.path("myresource").request().get(String.class);assertEquals("知道了!", responseMsg);}}
Is it possible to have a Jersey REST service standalone in a java application? Every example I find is in the context of a webcontainer (web.xml).
Thanks for help!
解决方案Just follow the Jersey docs getting started. It creates a standalone using Grizzly server. With Maven, you can easily create it with the following archetype command.
mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2
-DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false
-DgroupId=com.example -DartifactId=simple-service -Dpackage=com.example
-DarchetypeVersion=2.27
The last argument -DarchetypeVersion
can be replaced with any 2.x version you want. Currently, the latest is 2.27.
After running this command, you will have a generated main class you run from the command line.
Another way is to use Spring Boot (which a lot of people are using), you can check out the official example and check out the docs
Update
If you're not using Maven, below are all the jars that are pulled in with the archetype. Most are included in the Jersey RI bundle you download from the docs. Just figure out the ones that are not in the bundle and search for those.
I think the ones not included in the RI bundle DL, are
- grizzly-framework
- grizzly-http
- grizzly-http-server
- jersey-container-grizzly2-http
And here are all the generated classes from the archetype.
com.example.MyResource
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
Hit 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);
}
}
相关文章