【问题标题】:Error 404 - the requested resource is not available错误 404 - 请求的资源不可用
【发布时间】:2022-01-06 05:07:54
【问题描述】:

我对 Java 很陌生,我正在尝试创建一个非常简单的 Rest API 来通过 GET 方法返回一个字符串。我经历过无数教程,但每次使用我的项目时都会出现上述错误

url - http://localhost:8080/orchestrator/api/service/status

我使用 ApplicationConfig.java - ApplicationPath 文件来表示映射:

@javax.ws.rs.ApplicationPath("/api")
public class ApplicationConfig extends Application {

@Override
public Set<Class<?>> getClasses() {
    Set<Class<?>> resources = new java.util.HashSet<>();
    addRestResourceClasses(resources);
    return resources;
}

/**
 * Do not modify addRestResourceClasses() method.
 * It is automatically populated with
 * all resources defined in the project.
 * If required, comment out calling this method in getClasses().
 */
private void addRestResourceClasses(Set<Class<?>> resources) {
    resources.add(com.orchestrator.Services.class);
} }

通过 XAMPP v3.3.0 将 Netbeans 12.6 与 Tomcat 一起使用;我有一个 web.xml,但显然我不需要它,因为我正在使用 ApplicationConfig.java,但我会发布我制作的那个。

<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>.com.sun.jerysey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.orchestrator.Services</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

Services.java

@Path("/service")
public class Services {

@Context
private UriInfo context;

public Services() {
}

@Path("/status")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getHtml() {
    //TODO return proper representation object
    return "connected";
}
}

项目结构: image of folder structure

我缺少什么来返回“连接”文本,我也尝试过使用邮递员来返回,同样的结果。

问候

【问题讨论】:

  • 请给我看看,你的 web.xml 是什么样子的?
  • web.xml 是主帖中的第二个代码块。进一步探索表明我的项目正在部署到 tomcat -> work -> Catalina -> localhost。这不应该在 webapps 中吗?

标签: java rest netbeans


【解决方案1】:

看来你叫错了路线。

您的 applicationPath 以 /api 开头,并且 不是/orchestrator/api

你可以试试这条路线http://localhost:8080/api/service/status

【讨论】:

猜你喜欢
  • 2021-11-25
  • 2014-04-27
  • 2014-01-31
  • 2012-09-12
  • 1970-01-01
  • 2017-08-02
  • 1970-01-01
  • 1970-01-01
  • 2018-11-01
相关资源
最近更新 更多