首页 > 解决方案 > 在 GlassFish 上创建和运行 RESTful Web 服务

问题描述

我在 GlassFish 服务器上创建了一个简单的 RESTful Web 服务,并根据本教程在 IntelliJ IDE 中运行它。根据提供的说明,这运行良好。我还有2个问题,

一个。本教程使用下面提供的服务类,

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("/helloworld")
public class HelloWorld {

    @GET
    @Produces("text/plain")
    public String getClichedMessage() {

        return "Hello World";
    }
}

我可以从URL提供的访问,

http://localhost:8080/AppointmentManager_war_exploded/helloworld

之后,我在同一目录中添加了一个新类,

@Path("/")
public class App {

    @GET
    @Produces("text/plain")
    public String getMessage() {

        return "Hello, Berlin";
    }
}

我希望"Hello, Berlin"从打开的 URL 中看到浏览器中的消息http://localhost:8080/AppointmentManager_war_exploded/,但是,相反,我得到了提供的错误,

HTTP Status 404 - Not Found
type Status report

messageNot Found

descriptionThe requested resource is not available.

GlassFish Server Open Source Edition 5.0

这里有什么问题?

湾。如何将 URL 的部分更改AppointmentManager_war_exploded为其他内容,例如appointment等?下面artifact提供了项目设置中的选项卡,

在此处输入图像描述

我对其进行了编辑,但是它的更改与预期的不符。

我在教程之后将项目更改为maven构建,但是,问题不是为此而创建的。如果有人有兴趣,您也可以尝试一下,因为它需要一分钟才能运行。

谢谢你。

标签: javarestmavenjerseyjax-rs

解决方案


第一的

我希望从打开的 URL http://localhost:8080/AppointmentManager_war_exploded/在浏览器中看到消息“Hello, Berlin” ,但是,相反,我得到了提供的错误

MyApplication教程提供的课程中,您还应该添加新课程:

@ApplicationPath("/")
public class MyApplication extends Application{
    @Override
    public Set<Class<?>> getClasses() {
        HashSet h = new HashSet<Class<?>>();
        h.add(HelloWorld.class);
        h.add(App.class);          // Add your new class here
        return h;
    }
}

然后您将能够看到预期的页面http://localhost:8080/AppointmentManager_war_exploded/

第二

如何将 URL AppointmentManager_war_exploded 的部分更改为其他内容,例如约会等?

URL 包含您的工件的名称AppointmentManager_war_exploded。此工件自动复制到 glassfish 应用程序目录。你可以检查一下glassfish\domains\domain1\applications\__internal。只需在此处的项目结构窗口中更改它:

在此处输入图像描述

更新

不要忘记在应用程序的配置设置中更改启动 URL:

在此处输入图像描述


推荐阅读