JavaEE 应用程序如何将可以通过浏览器访问的 RESTEasy 服务的 url 放在一起?

How JavaEE application puts together the url of a RESTEasy service it can be reached via browser?

我开始玩 JavaEE 的东西,尤其是 RESTEasy,我试着把一个由 maven 打包的小服务放在一起。服务已完成,ear 包已创建并部署到 WildFly 10 服务器。没有错误,但我无法 reach/call 服务方法。

我在这里经历了很多问题和答案,根据它们,我的应用程序应该可以运行并且我应该可以调用它。一件事我不知道,到目前为止还没有找到任何文档,说明如何创建到达应用程序的 url。

这里的主要问题:这个url创作的规则是什么?我在哪里可以找到它?

到目前为止,我看到以下参数可以影响url:

让我们看看我的申请。

我的应用程序包含 3 个 Maven 模块。主模块(pom 模块)有两个模块,a,ear 包模块,b,rest api 模块创建war 文件。 Pom.xml 个文件如下。

Maven 模块:

最终,ear 文件 如下所示:

没有 web.xml 文件,也没有 jboss.xml 文件。

根据一些文章和示例,Currency/Currencies 方法应该可以使用以下 urls(它们都不起作用):

仅仅部署 war 文件也行不通。默认情况下,该文件的名称很丑:digitallibrary.dataservice.masterdata.rest.api-1.0-SNAPSHOT.war。成功部署后,端点应在此处可用:

没用。

Wildfly 说了以下内容,这可能意味着可以通过以下 urls 访问该应用程序(我只是猜测...),但它们都不起作用...

Wildfly 的部署日志。

19:58:17,673 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-5) WFLYSRV0027: Starting deployment of "MasterData.Dataservice.ear" (runtime-name: "MasterData.Dataservice.ear")
19:58:17,682 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-4) WFLYSRV0207: Starting subdeployment (runtime-name: "MasterData.Rest.Api.war")
19:58:17,712 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 111) WFLYUT0021: Registered web context: /
19:58:17,727 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 1) WFLYSRV0010: Deployed "MasterData.Dataservice.ear" (runtime-name : "MasterData.Dataservice.ear")

服务器 运行 并且没有错误。

package app;

import endpoints.CurrencyEndpoint;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by SayusiAndo on 6/9/2017.
 */
public class MasterDataRestEndpointApplication extends Application {

    private Set<Object> singletons = new HashSet<Object>();

    public MasterDataRestEndpointApplication() {
        singletons.add(new CurrencyEndpoint());
    }

    @Override
    public Set<Class<?>> getClasses() {
        HashSet<Class<?>> set = new HashSet<Class<?>>();
        return set;
    }

    @Override
    public Set<Object> getSingletons() {
        return singletons;
    }
}

package endpoints;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;

/**
 * Created by SayusiAndo on 6/9/2017.
 */
@Path("/currency")
public class CurrencyEndpoint {

    @GET
    @Path("/currencies")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getCurrencies() {
        ArrayList list = new ArrayList<String>();
        list.add("currency1");
        list.add("currency2");

        return Response
                .status(Response.Status.OK)
                .entity(list)
                .build();
    }
}

POM模块pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sayusiando</groupId>
    <artifactId>digitallibrary.dataservice.masterdata</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>digitallibrary.dataservice.masterdata.rest.api</module>
        <module>digitallibrary.dataservice.masterdata.package.ear</module>
    </modules>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.wildfly.plugins</groupId>
                <artifactId>wildfly-maven-plugin</artifactId>
                <version>1.2.0.Alpha4</version>
            </plugin>
        </plugins>
    </build>

</project>

EAR 模块pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>digitallibrary.dataservice.masterdata</artifactId>
        <groupId>com.sayusiando</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>ear</packaging>

    <artifactId>digitallibrary.dataservice.masterdata.package.ear</artifactId>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.10.1</version>
                <configuration>
                    <finalName>MasterData.Dataservice</finalName>
                    <modules>
                        <webModule>
                            <groupId>com.sayusiando</groupId>
                            <artifactId>digitallibrary.dataservice.masterdata.rest.api</artifactId>
                            <bundleFileName>MasterData.Rest.Api.war</bundleFileName>
                            <contextRoot>/</contextRoot>
                        </webModule>
                    </modules>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.sayusiando</groupId>
            <artifactId>digitallibrary.dataservice.masterdata.rest.api</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>war</type>
        </dependency>
    </dependencies>


</project>

REST 模块pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>digitallibrary.dataservice.masterdata</artifactId>
        <groupId>com.sayusiando</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>war</packaging>
    <artifactId>digitallibrary.dataservice.masterdata.rest.api</artifactId>

    <dependencies>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.1-m07</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-servlet-initializer</artifactId>
            <version>3.1.2.Final</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>3.1.2.Final</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxb-provider</artifactId>
            <version>3.1.2.Final</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

已生成 application.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE application PUBLIC
    "-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN"
    "http://java.sun.com/dtd/application_1_3.dtd">
<application>
  <display-name>digitallibrary.dataservice.masterdata.package.ear</display-name>
  <module>
    <web>
      <web-uri>MasterData.Rest.Api.war</web-uri>
      <context-root>/</context-root>
    </web>
  </module>
</application>

我犯了一些错误,但我学到了一些东西。

第一个错误:

出于某种原因,我删除了 RestEasy 应用程序路径上的 @ApplicationPath() 注释。没有这个,web.xml Wildfly 不知道如何处理这些 classes。所以他们没有注册。

放回 @ApplicationPath() 注释并删除 web.xml 使情况好转。

第二个错误:

出于某种原因 (:)),我检查了 http://localhost:8000 instead of http://localhost:8080。后者是 WildFly 的网站。第一个没什么。

回答我的问题:

  • EAR 的名称和 WAR 文件在这种情况下无关紧要
  • url根据以下内容创建:http://{host}:{port}/{applicationPathvalue}/{pathValueOfClass}/{pathValueOfMethod}

就我而言:

  • {host}: 本地主机
  • {port}: 8080
  • {applicationPathValue}: api(检查下面的代码示例)
  • {pathValueOfClass}:货币(检查我问题中的代码)
  • {pathValueOfMethod}:货币(检查我问题中的代码)

    @ApplicationPath("/api") public class MasterDataRestEndpointApplication 扩展应用程序 {

    private Set<Object> singletons = new HashSet<Object>();
    
    public MasterDataRestEndpointApplication() {
        singletons.add(new CurrencyEndpoint());
    }
    
    @Override
    public Set<Class<?>> getClasses() {
        HashSet<Class<?>> set = new HashSet<Class<?>>();
        return set;
    }
    
    @Override
    public Set<Object> getSingletons() {
        return singletons;
    }
    

    }