简体   繁体   English

使用 JAX-RS tomcat 服务器返回“请求的资源不可用”的 REST

[英]REST with JAX-RS tomcat server returning "requested resource is not available"

I was following short tutorial on creating REST api using JAX-RS.我正在学习使用 JAX-RS 创建 REST api 的简短教程 I am using Tomcat server v7.0.我正在使用 Tomcat 服务器 v7.0。 When I run the application on the server I get error 404-requested resource is not available.当我在服务器上运行应用程序时,我收到错误 404 请求的资源不可用。 The project is Maven based, and my pom.xml file includes the following line该项目基于 Maven,我的 pom.xml 文件包含以下行

  <properties>
    <failOnMissingWebXml>false</failOnMissingWebXml>
 </properties>

I do not have web.xml file as a result.结果我没有 web.xml 文件。 That was how the tutorial was achieved.这就是教程的实现方式。 I do not have index.html/jsp file.我没有 index.html/jsp 文件。 I have created two classes RESTconfig.java and BookResources.java我创建了两个类RESTconfig.javaBookResources.java

...import statements 
@ApplicationPath("api")
public class RESTconfig extends Application {

}

...import statements
@Path("books")
public class BookResources {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String books() {
        return "Hello world";
    }
}

My pom.xml file looks like this我的pom.xml文件看起来像这样

<groupId>com.dere</groupId>
  <artifactId>myrest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
    <properties>
    <failOnMissingWebXml>false</failOnMissingWebXml>
  </properties>
  <dependencies>
  <dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
    <scope>provided</scope>
 </dependency>

Once I run the application on the server and go to http://localhost:9090 I am able to see Tomcat home page, but if try to get data http://localhost:9090/myrest/api/books I get the 404 error, ie requested resource is not available, I mentioned above.一旦我在服务器上运行应用程序并转到http://localhost:9090我就可以看到 Tomcat 主页,但是如果尝试获取数据http://localhost:9090/myrest/api/books我得到 404错误,即请求的资源不可用,我在上面提到过。 Most of the examples or usage I saw online involve using web.xml and providing root of the application and using a servlet.我在网上看到的大多数示例或用法都涉及使用 web.xml 并提供应用程序的根目录和使用 servlet。 This is my first exposure to building REST api.这是我第一次接触构建 REST api。 I may have misunderstood the whole thing or I skipped some step.我可能误解了整件事,或者我跳过了一些步骤。 I look forward for your help.我期待着你的帮助。 I am using Eclipse Photon for Java EE我正在为 Java EE 使用 Eclipse Photon

Look at this看这个

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
</dependency>

This is nothing more than basically a bunch of interfaces for the EE spec.这只不过是 EE 规范的一堆接口。 There is no implementation.没有实施。 Java EE servers will have the implementation. Java EE 服务器将具有该实现。 Tomcat is not an EE server. Tomcat 不是 EE 服务器。 The only part of the EE spec it will definitely have the implementation for is Servlets and JSP (the web profile).它肯定会实现的 EE 规范的唯一部分是 Servlets 和 JSP(Web 配置文件)。 If you want an EE server, checkout Glassfish or Wildfly.如果您想要 EE 服务器,请查看 Glassfish 或 Wildfly。

You are trying to work with the JAX-RS spec, where Tomcat for sure by default does not have an implementation for.您正在尝试使用 JAX-RS 规范,默认情况下 Tomcat 肯定没有实现。 So you need to add that implementation.所以你需要添加那个实现。 The easiest implementation, IMO to get started with, is Jersey.最简单的实施,IMO 开始,是 Jersey。 You can simply add this dependency您可以简单地添加此依赖项

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.25.1</version>
</dependency>

and it will get you up and running.它会让你启动并运行。 Keep the Jersey User Guide handy.随身携带泽西岛用户指南 It will come in use.它将投入使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM