在Gradle中将Jersey与Open Liberty结合使用

时间:2019-04-05 21:21:47

标签: jersey-2.0 open-liberty

我使用gradle和Open Liberty进行了简单的设置。来自此博客文章https://openliberty.io/blog/2018/12/14/microprofile21-18004.html 它说

“ Open Liberty现在具有对JAX-RS 2.1的反应性扩展,因此您可以使用来自Apache CXF和Jersey的提供程序”

但是我无法与Jersey一起使用

我已经尝试了build.gradle文件和server.xml的各种配置,但仍然无法使它正常工作并发现缺少的文档。

build.gradle

apply plugin: 'war'
apply plugin: 'liberty'
apply plugin: 'java-library'

group = 'x.y.z'
version = '1.0-SNAPSHOT'
description = "X Y Z"

sourceCompatibility = 1.8
targetCompatibility = 1.8
tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}



buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'net.wasdev.wlp.gradle.plugins:liberty-gradle-plugin:2.6.3'
    }
}

repositories {
    mavenCentral()
}

//required for compiling, testing and running the application
dependencies {

    implementation group: 'org.glassfish.jersey.core', name: 'jersey-client', version: '2.26'
    implementation group: 'org.glassfish.jersey.core', name: 'jersey-common', version: '2.26'
    implementation group: 'org.glassfish.jersey.core', name: 'jersey-server', version: '2.26'

    providedCompile group:'javax.servlet', name:'javax.servlet-api', version:'3.1.0'
    testCompile group:'commons-httpclient', name:'commons-httpclient', version:'3.1'
    testCompile group:'junit', name:'junit', version:'4.12'

    libertyRuntime group:'io.openliberty', name:'openliberty-runtime', version:'19.0.0.3'

    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api 'org.apache.commons:commons-math3:3.6.1'

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation 'com.google.guava:guava:27.0.1-jre'
    testImplementation 'junit:junit:4.12'
}

//Extra properties for project level 
ext {
    appName = project.name
    testServerHttpPort = 9080
    testServerHttpsPort = 9443
    warContext = appName
}

liberty {
    server {
        name = "${appName}Server"
        configFile = file("src/main/liberty/config/server.xml")
        bootstrapProperties = ['default.http.port': testServerHttpPort,
                               'default.https.port': testServerHttpsPort,
                               'app.context.root': warContext]
        packageLiberty {
            archive = "$buildDir/${appName}.zip"
            include = "usr"
        }
    }
}

war {
    archiveName = "${baseName}.${extension}"
}

//unit test configuration
test {
    reports.html.destination = file("$buildDir/reports/unit")
    reports.junitXml.destination = file("$buildDir/test-results/unit")
    exclude '**/it/**'
}

//integration test configuration
task integrationTest(type: Test) {
    group 'Verification'
    description 'Runs the integration tests.'
    reports.html.destination = file("$buildDir/reports/it")
    reports.junitXml.destination = file("$buildDir/test-results/it")
    include '**/it/**'
    exclude '**/unit/**'

    systemProperties = ['liberty.test.port': testServerHttpPort, 'war.name': warContext]
}

task openBrowser {
    description = 'Open browser to http://localhost:8080/${warContext}'
    doLast {
        java.awt.Desktop.desktop.browse
            "http://localhost:${testServerHttpPort}/${appName}".toURI()
    }
}

task openTestReport {
    description = 'Open browser to the test report'
    doLast {
        java.awt.Desktop.desktop.browse file("$buildDir/reports/it/index.html").toURI()
    }
}

clean.dependsOn 'libertyStop'
check.dependsOn 'integrationTest'
integrationTest.dependsOn 'libertyStart'
integrationTest.finalizedBy 'libertyStop'
integrationTest.finalizedBy 'openTestReport'
libertyPackage.dependsOn 'libertyStop'

server.xml

<?xml version="1.0" encoding="UTF-8"?>
<server description="new server">
    <featureManager>
        <!--   <feature>jsp-2.3</feature> -->
        <!--  <feature>jaxrs-2.1</feature> -->
        <feature>servlet-4.0</feature>
        <!--  
         -->
    </featureManager>
    <httpEndpoint id="defaultHttpEndpoint"
                  httpPort="${default.http.port}"
                  httpsPort="${default.https.port}" />
    <applicationManager autoExpand="true"/>
    <webApplication contextRoot="${app.context.root}" location="${app.context.root}.war" />
</server>

HelloWorldService

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/hello")
public class HelloWorldService {

    @GET
    @Path("/{param}")
    public Response getMsg(@PathParam("param") String msg) {
        String output = "Jersey say : " + msg;
        return Response.status(200).entity(output).build();
    }

}

当我尝试击中终点时,我会得到404

curl localhost:9080/XYZ/hello/jersey

错误404:java.io.FileNotFoundException:SRVE0190E:找不到文件:/ hello / jersey

非常感谢任何帮助!

0 个答案:

没有答案