jax-rs tomcat maven原始服务器未找到目标资源的当前表示,或不愿意透露存在

时间:2019-06-12 19:46:19

标签: maven jax-rs tomcat9

我使用JAX-RS。我有一个Maven项目和Tomcat服务器。我用路径/ books java类。我也使用jersey 1.x。在Tomcat控制台中,我可以看到我的应用程序已部署。

网址http://localhost:8080/rest-server可以正常工作,但是当我执行网址时

http://localhost:8080/rest-server/books

我得到了错误

HTTP 404



The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

这是我的web.xml文件

在我定义的web.xml中

<url-pattern>/rest-server/*</url-pattern> 





 <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
      <display-name>rest-server</display-name>
    <servlet>
    <servlet-name>Test Jersey Service</servlet-name>
    <!-- For Jersey 1.x -->
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <!-- For Jersey 2.x -->
    <!--  <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> -->
    <init-param>
        <!-- For Jersey 1.x -->
    <param-name>com.sun.jersey.config.property.packages</param-name> 
    <!-- For Jersey 2.x -->
    <!--  <param-name>jersey.config.server.provider.packages</param-name> -->
    <param-value>com.readlearncode.dukesbookshop.restserver.rest</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>Test Jersey Service </servlet-name>
    <url-pattern>/rest-server/*</url-pattern>
    </servlet-mapping>
    </web-app>

这是我的pom.xml

在pom.xml中

<name>rest-server</name>

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.readlearncode</groupId>
        <artifactId>dukesbookshop</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>rest-server</artifactId>
    <packaging>war</packaging>

    <name>rest-server</name>
    <url>http://localhost:8080</url>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <java.version>1.8</java.version>
        <javaee-api.version>7.0</javaee-api.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>${javaee-api.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
        <!-- For Jersey 1.x -->
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-server</artifactId>
            <version>1.19</version>
        </dependency>
        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-servlet</artifactId>
            <version>1.19</version>
        </dependency> 
    </dependencies>

    <build>
        <finalName>rest-server</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                 <configuration>
                    <url>http://localhost:8080/manager/text</url>
                    <path>/rest-server</path>
                    <!-- Set update to true to avoid exist war package can not be override error -->
                    <update>true</update>
                    <!-- Because deploy this maven project using plugin in pom so use the manager-script role user. -->
                    <username>tomcat</username>
                    <password>tomcat</password>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <skipTests>true</skipTests>
                    <systemPropertyVariables>
                        <!-- This is needed to tell the unit tests which profile we are running. -->
                    </systemPropertyVariables>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

这是我的Java服务

此类具有@Path / books

 package com.readlearncode.dukesbookshop.restserver.rest;

    import javax.ejb.EJB;
    import javax.ejb.Stateless;
    import javax.ws.rs.Consumes;
    import javax.ws.rs.GET;
    import javax.ws.rs.POST;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.GenericEntity;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.core.Response;

    import java.util.List;
    import java.util.Optional;

    import javax.annotation.*;

    import com.readlearncode.dukesbookshop.restserver.domain.Book;
    import com.readlearncode.dukesbookshop.restserver.infrastructure.BookRepository;

    /**
     * @author Alex Theedom www.readlearncode.com
     * @version 1.0
     */
    @Stateless
    @Path("/books")
    public class BookResource {

        @EJB
        private BookRepository bookrepository;
        @GET
        @Produces(MediaType.APPLICATION_JSON)
        public Response getAllBooks() {

            List<Book> books = bookrepository.getAll();
            GenericEntity<List<Book>> bookWrapper =  new GenericEntity<List<Book>>(books) {};

            return Response.ok(bookWrapper).build();

        }

        @POST
        @Produces(MediaType.APPLICATION_JSON)
        @Consumes(MediaType.APPLICATION_JSON)
        public Response saveBook(final Book book) {
            Book persistedBook = bookrepository.saveBook(book);

            return Response.ok(persistedBook).build();

        }

        @GET
        @Produces(MediaType.APPLICATION_JSON)
        @Path("{isbn:\\d{9}[\\d:X]$}")
        public Response getBookbyIsbn(final @PathParam("isbn") String isbn) {

            Optional<Book> book = bookrepository.getByISBN(isbn);
            if (book.isPresent()) {
                return Response.ok(book.get()).build();
            }
            return Response.noContent().build();

        }
    }

0 个答案:

没有答案