泽西岛REST风格的Web服务gradle设置 [英] Jersey RESTful web service gradle setup

查看:113
本文介绍了泽西岛REST风格的Web服务gradle设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坚持使用球衣库为RESTful Web服务创建gradle项目。项目配置应该能够在码头应用程序服务器内启动服务。我已经找到资源: https://github.com/ziroby/jetty-gradle-hello -world



这个解决方案的问题在于,它使用过时的球衣版本。我至少需要版本2(首选最新版本2.14)。我试图在maven central上搜索新版本,但是在版本2中,很多工件名称发生了变化,我无法正确配置它。



编辑:
我的项目中并不特别需要码头服务器。它可以是任何适用于测试和调试我的应用的应用服务器。



编辑:(by peeskillet) - 来自链接的代码



build

  apply plugin:'java'
apply插件:'jetty'

存储库{
mavenCentral()
}
依赖关系{
testCompile'junit:junit:4.11'
testCompile 'org.hamcrest:hamcrest-all:1.3'
testCompile'c​​om.sun.jersey:jersey-client:1.17.1'
testCompile'c​​om.sun.jersey:jersey-core:1.17.1 '
compile'c​​om.sun.jersey:jersey-core:1.17.1'
compile'c​​om.sun.jersey:jersey-server:1.17.1'
compile'c​​om.sun .jersey:jersey-servlet:1.17.1'
}
test {
exclude'** / * IntegrationTest *'
}

任务集成测试(类型:Test){
包含'** / * IntegrationTest *'
doFirst {
jettyRun.httpPort = 8080 //测试端口
jettyRun.daemon = tr ue
jettyRun.execute()
}
doLast {
jettyStop.stopPort = 8091 //停止信号端口
jettyStop.stopKey ='stopKey'
jettyStop.execute()
}
}

测试

  public class HelloIntegrationTest {
private static String HELLO_URL =http:// localhost:8080 / hello;

@Test
public void testHello()抛出Exception {
Client client = Client.create();
WebResource webResource = client.resource(HELLO_URL);
String response = webResource.get(String.class);

assertThat(response,is(Hello,World!));


$ / code $ / pre

$ p

  @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< /参数值> - >
<! - < / init-param> - >
1< / load-on-startup>
< / servlet>
< servlet-mapping>
< servlet-name> HelloWorldServlet< / servlet-name>
< url-pattern> / *< / url-pattern>
< / servlet-mapping>
< / web-app>


解决方案

第一个



摆脱您目前拥有的所有泽西岛依赖项



 依赖关系{
testCompile'junit:junit:4.11'
testCompile'org.hamcrest:hamcrest-all:1.3'
+ ------------- ===== == JUNK ======= ---------------- +
| testCompile'c​​om.sun.jersey:jersey-client:1.17.1'|
|编译'com.sun.jersey:jersey-core:1.17.1'|
|编译'com.sun.jersey:jersey-server:1.17.1'|
|编译'com.sun.jersey:jersey-servlet:1.17.1'|
+ --------------------------------------------- ------ +
}

以下是 em>只有一个你需要获得基本功能



 依赖关系{
testCompile 'junit:junit:4.11'
testCompile'org.hamcrest:hamcrest-all:1.3'
+ -------------------- == ======= GOLDEN ======== ------------------------- +
|编译'org.glassfish.jersey.containers:jersey-container-servlet:2.14'|
+ --------------------------------------------- --------------------------- +
}



第二



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>
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()抛出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!));


$ / code $ / pre

$ hr

这已经通过链接项目的克隆进行了测试。



其他资源:






更新



对于JSON支持使用

  org.glassfish.jersey.media:jersey-media-json-jackson:2.14 

无需额外配置即可工作。


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>

解决方案

First

Get rid of all the Jersey dependencies you currently have

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'    |
    +---------------------------------------------------+
}

Below is the only only one you need to get the basic functionality

dependencies {
    testCompile 'junit:junit:4.11'
    testCompile 'org.hamcrest:hamcrest-all:1.3'
   +-------------------- ========= GOLDEN ======== -------------------------+
   | compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.14'|
   +------------------------------------------------------------------------+
}

Second

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>

Third

Test

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!"));
    }
}


This has been tested with a clone of the linked project. Only changes are shown above.

Other Resources:


Update

For JSON support use

org.glassfish.jersey.media:jersey-media-json-jackson:2.14

No extra configuration is required for it to work.

这篇关于泽西岛REST风格的Web服务gradle设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆