java - 在 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
构建,但是,问题不是为此而创建的。如果有人有兴趣,您也可以尝试一下,因为它需要一分钟才能运行。
谢谢你。
解决方案
第一的
我希望从打开的 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:
推荐阅读
- java - 我可以使用 java 从 spark UDF 返回一个 java 对象吗
- r - 如何根据公共列的计数从两个不同的数据帧中抽取样本?
- javascript - 字符 '+' 在 base64 编码数据中转换为 +
- javascript - 单击 datetimepicker 中的日期时获取值
- azure - 为 APIM 下的所有 API 实例创建请求速率限制
- d3.js - 如何突出显示多个圆环图 d3 的一部分
- distance - 如何在行为空间中报告从补丁到海龟的距离?
- awk - 涉及awk的一行代码分解
- java - RESTful Java 客户端将 JSON 响应转换为对象
- json - 从 JSON 中提取数据并使用 jq 将其作为新数据插入