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'| +------------------------------------------------------------------------+ }
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
不需要额外的配置即可工作。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)