Jersey RESTful web service gradle setup

前端 未结 1 1827
小鲜肉
小鲜肉 2020-12-03 05:11

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 j

相关标签:
1条回答
  • 2020-12-03 05:40

    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'|
       +------------------------------------------------------------------------+
       /* UPDATE */
       /* Starting Jersey version 2.26, you will also need the following */
       /* compile 'org.glassfish.jersey.inject:jersey-hk2:2.26' */
    }
    

    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:

    • What exactly is the ResourceConfig?
    • More information on deployment options
    • More information in the Client API

    Update

    For JSON support use

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

    No extra configuration is required for it to work.

    0 讨论(0)
提交回复
热议问题