Jersey RESTful Web服务gradle设置
我坚持使用jersey库为RESTful Web服务创建一个gradle项目。 项目配置应该能够在jetty应用程序服务器中启动服务。 我已经找到了一个资源: https : //github.com/ziroby/jetty-gradle-hello-world
我的解决方案的问题是,它使用了过时的jersey版本。 我至少需要2版(最新的2.14版)。 我试图在maven central上搜索新版本,但在版本2中,很多工件名称发生了变化,我无法正确配置它。
编辑:我的项目中并不特别需要Jetty服务器。 它可以是任何应用程序服务器,适用于测试和调试我的应用程序。 我也在生产中使用docker,所以使用docker会很不错。
编辑 :(通过peeskillet) – 链接代码
建立
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中
Jetty Gradle Hello World HelloWorldServlet com.sun.jersey.spi.container.servlet.ServletContainer com.sun.jersey.config.property.packages com.ziroby.hello.webapp <!-- --> <!-- com.sun.jersey.api.json.POJOMappingFeature --> <!-- true --> <!-- --> 1 HelloWorldServlet /*
第一
摆脱目前所有的泽西岛依赖关系
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' | +---------------------------------------------------+ }
以下是获得基本function所需的唯一一个
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中
Jersey Web Application org.glassfish.jersey.servlet.ServletContainer jersey.config.server.provider.packages com.ziroby.hello.webapp 1 Jersey Web Application /*
第三
测试
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!")); } }
这已经通过链接项目的克隆进行了测试。 上面只显示了更改。
其他资源:
- 有关部署选项的更多信息
- 客户端API中的更多信息
更新
对于JSON支持使用
org.glassfish.jersey.media:jersey-media-json-jackson:2.14
无需额外配置即可运行。
- 具有类型参数的Guice模块
- 线程“main”中的exceptionjava.lang.NoClassDefFoundError:org / apache / hadoop / hbase / HBaseConfiguration
- gradle – 生成后复制文件
- Gradle不包含可选的依赖项
- React-native build error Android – java.lang.UnsupportedClassVersionError:com / android / build / gradle / AppPlugin:不支持的major.minor版本52.0
- 在Gradle Android Studio中排除一个类文件
- 如何在没有安装maven和java的情况下从gradle执行maven插件
- 将glassfish javax.persistence添加到gradle项目中
- gradle存储库指向具有多个库的本地目录
- IntelliJ找不到任何LibGDX包?
- 如何配置IntelliJ / gradle以使用dagger 2.0