泽西 RESTful 网络服务 gradle 设置
Jersey RESTful web service gradle setup
我一直坚持使用 jersey 库为 RESTful 网络服务创建 gradle 项目。项目配置应该能够在码头应用程序服务器内启动服务。我已经找到资源:https://github.com/ziroby/jetty-gradle-hello-world
我对该解决方案的问题是,它使用了过时版本的球衣。我至少需要版本 2(最好是最新的 2.14)。我试图在 maven central 上搜索新版本,但是在版本 2 中,很多工件名称发生了变化,我无法正确配置它。
编辑:
我的项目中并不特别需要码头服务器。它可以是任何应用服务器,适合测试和调试我的应用程序。我也在生产中使用码头,所以使用码头会很好。
编辑:(通过 peeskillet)- 来自 link
的代码
建造
apply plugin: 'java'
apply plugin: 'jetty'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.11'
testCompile 'org.hamcrest:hamcrest-all:1.3'
testCompile 'com.sun.jersey:jersey-client:1.17.1'
testCompile 'com.sun.jersey:jersey-core:1.17.1'
compile 'com.sun.jersey:jersey-core:1.17.1'
compile 'com.sun.jersey:jersey-server:1.17.1'
compile 'com.sun.jersey:jersey-servlet:1.17.1'
}
test {
exclude '**/*IntegrationTest*'
}
task integrationTest(type: Test) {
include '**/*IntegrationTest*'
doFirst {
jettyRun.httpPort = 8080 // Port for test
jettyRun.daemon = true
jettyRun.execute()
}
doLast {
jettyStop.stopPort = 8091 // Port for stop signal
jettyStop.stopKey = 'stopKey'
jettyStop.execute()
}
}
测试
public class HelloIntegrationTest {
private static String HELLO_URL = "http://localhost:8080/hello";
@Test
public void testHello() throws Exception {
Client client = Client.create();
WebResource webResource = client.resource(HELLO_URL);
String response = webResource.get(String.class);
assertThat(response, is("Hello, World!"));
}
}
资源
@Path("/hello")
public class HelloWebapp {
private static HelloWorldService helloWorldService = new HelloWorldService();
@GET()
public String hello() {
return helloWorldService.sayHello();
}
}
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Jetty Gradle Hello World</display-name>
<servlet>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.ziroby.hello.webapp</param-value>
</init-param>
<!-- <init-param> -->
<!-- <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> -->
<!-- <param-value>true</param-value> -->
<!-- </init-param> -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
第一个
摆脱您当前拥有的所有 Jersey 依赖项
dependencies {
testCompile 'junit:junit:4.11'
testCompile 'org.hamcrest:hamcrest-all:1.3'
+------------- ======= JUNK ======= ----------------+
| testCompile 'com.sun.jersey:jersey-client:1.17.1' |
| compile 'com.sun.jersey:jersey-core:1.17.1' |
| compile 'com.sun.jersey:jersey-server:1.17.1' |
| compile 'com.sun.jersey:jersey-servlet:1.17.1' |
+---------------------------------------------------+
}
以下是仅您需要获得基本功能的唯一一个
dependencies {
testCompile 'junit:junit:4.11'
testCompile 'org.hamcrest:hamcrest-all:1.3'
+-------------------- ========= GOLDEN ======== -------------------------+
| compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.14'|
+------------------------------------------------------------------------+
/* UPDATE */
/* Starting Jersey version 2.26, you will also need the following */
/* compile 'org.glassfish.jersey.inject:jersey-hk2:2.26' */
}
第二个
web.xml
<web-app>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.ziroby.hello.webapp</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
第三
测试
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.Test;
public class HelloIntegrationTest {
private static String HELLO_URL = "http://localhost:8080/hello";
@Test
public void testHello() throws Exception {
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target(HELLO_URL);
String response = webTarget.request().get(String.class);
System.out.println(response);
assertThat(response, is("Hello, World!"));
}
}
这已经通过链接项目的克隆进行了测试。上面只显示了变化。
其他资源:
更新
为JSON支持使用
org.glassfish.jersey.media:jersey-media-json-jackson:2.14
无需额外配置即可运行。
我一直坚持使用 jersey 库为 RESTful 网络服务创建 gradle 项目。项目配置应该能够在码头应用程序服务器内启动服务。我已经找到资源:https://github.com/ziroby/jetty-gradle-hello-world
我对该解决方案的问题是,它使用了过时版本的球衣。我至少需要版本 2(最好是最新的 2.14)。我试图在 maven central 上搜索新版本,但是在版本 2 中,很多工件名称发生了变化,我无法正确配置它。
编辑: 我的项目中并不特别需要码头服务器。它可以是任何应用服务器,适合测试和调试我的应用程序。我也在生产中使用码头,所以使用码头会很好。
编辑:(通过 peeskillet)- 来自 link
的代码建造
apply plugin: 'java'
apply plugin: 'jetty'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.11'
testCompile 'org.hamcrest:hamcrest-all:1.3'
testCompile 'com.sun.jersey:jersey-client:1.17.1'
testCompile 'com.sun.jersey:jersey-core:1.17.1'
compile 'com.sun.jersey:jersey-core:1.17.1'
compile 'com.sun.jersey:jersey-server:1.17.1'
compile 'com.sun.jersey:jersey-servlet:1.17.1'
}
test {
exclude '**/*IntegrationTest*'
}
task integrationTest(type: Test) {
include '**/*IntegrationTest*'
doFirst {
jettyRun.httpPort = 8080 // Port for test
jettyRun.daemon = true
jettyRun.execute()
}
doLast {
jettyStop.stopPort = 8091 // Port for stop signal
jettyStop.stopKey = 'stopKey'
jettyStop.execute()
}
}
测试
public class HelloIntegrationTest {
private static String HELLO_URL = "http://localhost:8080/hello";
@Test
public void testHello() throws Exception {
Client client = Client.create();
WebResource webResource = client.resource(HELLO_URL);
String response = webResource.get(String.class);
assertThat(response, is("Hello, World!"));
}
}
资源
@Path("/hello")
public class HelloWebapp {
private static HelloWorldService helloWorldService = new HelloWorldService();
@GET()
public String hello() {
return helloWorldService.sayHello();
}
}
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Jetty Gradle Hello World</display-name>
<servlet>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.ziroby.hello.webapp</param-value>
</init-param>
<!-- <init-param> -->
<!-- <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> -->
<!-- <param-value>true</param-value> -->
<!-- </init-param> -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
第一个
摆脱您当前拥有的所有 Jersey 依赖项
dependencies {
testCompile 'junit:junit:4.11'
testCompile 'org.hamcrest:hamcrest-all:1.3'
+------------- ======= JUNK ======= ----------------+
| testCompile 'com.sun.jersey:jersey-client:1.17.1' |
| compile 'com.sun.jersey:jersey-core:1.17.1' |
| compile 'com.sun.jersey:jersey-server:1.17.1' |
| compile 'com.sun.jersey:jersey-servlet:1.17.1' |
+---------------------------------------------------+
}
以下是仅您需要获得基本功能的唯一一个
dependencies {
testCompile 'junit:junit:4.11'
testCompile 'org.hamcrest:hamcrest-all:1.3'
+-------------------- ========= GOLDEN ======== -------------------------+
| compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.14'|
+------------------------------------------------------------------------+
/* UPDATE */
/* Starting Jersey version 2.26, you will also need the following */
/* compile 'org.glassfish.jersey.inject:jersey-hk2:2.26' */
}
第二个
web.xml
<web-app>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.ziroby.hello.webapp</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
第三
测试
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.Test;
public class HelloIntegrationTest {
private static String HELLO_URL = "http://localhost:8080/hello";
@Test
public void testHello() throws Exception {
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target(HELLO_URL);
String response = webTarget.request().get(String.class);
System.out.println(response);
assertThat(response, is("Hello, World!"));
}
}
这已经通过链接项目的克隆进行了测试。上面只显示了变化。
其他资源:
更新
为JSON支持使用
org.glassfish.jersey.media:jersey-media-json-jackson:2.14
无需额外配置即可运行。