HelloWorld示例中的JaxRS路径

时间:2016-09-05 20:20:19

标签: glassfish wildfly restful-url

这是Restful服务的JEE 7教程中的第一个示例,只需将“hello world”打印到屏幕上即可。本教程配置为在Glassfish中运行,但我在Eclipse和Wildfly中测试它。当我启动应用程序Wildfly时,它显示“http://localhost:8080/hello/”,错误消息是:

Failed to execute: javax.ws.rs.NotFoundException: RESTEASY003210: Could not find resource for full path: http://localhost:8080/hello/

然后我运行此网址:“http://localhost:8080/hello/helloworld”,它可以正常运行。原始教程下面是Glassfish的配置,因此可以直接执行“http://localhost:8080/hello”来运行示例。

如何更改配置或源代码,以便我可以直接运行“http://localhost:8080/hello”并在Wildfly和Eclipse中附加“helloworld”?原始教程甚至没有要配置的web.xml文件。相反,它使用nbactions.xml来进行配置。

<action>
            <actionName>profile</actionName>
            <goals>
                <goal>package</goal>
            </goals>
            <properties>
                <netbeans.deploy>true</netbeans.deploy>
                <netbeans.deploy.profilemode>true</netbeans.deploy.profilemode>
                <netbeans.deploy.clientUrlPart>/helloworld</netbeans.deploy.clientUrlPart>
            </properties>
        </action>

HelloWorldApplication.java

@ApplicationPath("/")
public class HelloApplication extends Application {

}

HelloWorld.java

@Path("helloworld")
public class HelloWorld {
    @Context
    private UriInfo context;

    /** Creates a new instance of HelloWorld */
    public HelloWorld() {
    }

    /**
     * Retrieves representation of an instance of helloWorld.HelloWorld
     * @return an instance of java.lang.String
     */
    @GET
    @Produces("text/html")
    public String getHtml() {
        return "<html lang=\"en\"><body><h1>Hello, World!!</h1></body></html>";
    }

    /**
     * PUT method for updating or creating an instance of HelloWorld
     * @param content representation for the resource
     */
    @PUT
    @Consumes("text/html")
    public void putHtml(String content) {
    }
}

的pom.xml

<modelVersion>4.0.0</modelVersion>
    <groupId>org.glassfish.javaeetutorial</groupId>
    <artifactId>hello</artifactId>
    <packaging>war</packaging>
    <name>hello</name>

    <parent>
      <groupId>org.glassfish.javaeetutorial</groupId>
      <artifactId>jaxrs</artifactId>
      <version>7.0.5</version>
    </parent>

1 个答案:

答案 0 :(得分:1)

应用程序路径首先由HelloWorldApplication.java类注释

设置
@ApplicationPath("/")

这意味着您的应用程序将在部署的根上下文中提供,因此您只需转到http://localhost:8080/hello即可访问应用程序中的任何资源。

您在HelloWorld.java中定义的其余资源通过另一个类注释在应用程序中设置其路径:

@Path("helloworld")

因此JAX-RS将使用@ApplicationPath和资源@Path的组合路径使此资源可用。您可以自己看看,您可以将上面的@Path更改为:

@Path("/")

您的资源的路径将是context-path + ApplicationPath + Path,或者换句话说,您的方案是your-app-name + nothing + nothing

将应用程序部署到GlassFish时,您甚至可以将上下文路径设置为根上下文,这样您甚至不需要在URL中指定hello