I am stuck with creating a gradle project for a RESTful web service using the jersey library. The project configuration should be capable of launching the service inside a jetty application server. I already found a resource: https://github.com/ziroby/jetty-gradle-hello-world
My problem with that solution is, that it uses an outdated version of jersey. I need at least version 2(preferred latest 2.14). I tried to search for new versions on maven central, but in version 2 a lot of artifact names changed, and I am not able to configure it correctly.
Edit: I do not specifically need a jetty server in my project. It can be any application server, which is suitable for testing and debugging my app. I am using jetty also in production, so it would be nice to use jetty.
EDIT: (by peeskillet) - Code from link
build
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()
}
}
Test
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!"));
}
}
Resource
@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>