映射上的REST api 404

时间:2015-12-21 16:19:38

标签: java rest java-ee glassfish jersey-2.0

我正在尝试使用glassfish 4服务器上的jersey创建一个rest api。 目前,每当我进行映射时,我都会收到错误404:

http://localhost:8080/LibraryRestTest/api/books

可悲的是,我的glassfish控制台日志中出现0错误。我也尝试在glassfish管理控制台上手动部署我的战争,导致同样的错误。

有没有我尚未实施的内容?

我在项目中创建的所有文件:

的glassfish-web.xml中

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app>
    <context-root>/LibraryRestTest</context-root>
</glassfish-web-app>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

     <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>
            org.glassfish.jersey.servlet.ServletContainer
        </servlet-class>
        <init-param>
            <param-name>
                jersey.config.server.provider.packages
            </param-name>
            <param-value>
                com.koen.library
            </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>
</web-app>

的pom.xml

<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>
    <groupId>com.koen</groupId>
    <artifactId>LibraryRestTest</artifactId>
    <version>0.0.1</version>
    <packaging>war</packaging>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <webXml>WebContent\WEB-INF\web.xml</webXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>
        <jersey.version>2.22</jersey.version>
        <parser.version>2.22</parser.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.koen.library</groupId>
            <artifactId>libraryJPA</artifactId>
            <version>0.0.1</version>
        </dependency>

        <!-- Jersey -->
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>${jersey.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>${parser.version}</version>
        </dependency>

    </dependencies>
</project>

BookResource.java

package com.koen.library;

import java.util.ArrayList;

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

import com.koen.library.pojo.Book;



@Path("books")
public class BookResource {

    @GET
    @Produces("application/json")
    public ArrayList<Book> getAll(){
        return new ArrayList<Book>(){{add(new Book(6, "The Brothers Karamazov", "Fyodor Dostoevsky"));}};
    }

    @GET
    @Path("/{id}")
    public Book getById(@PathParam ("id") int id){

        return null;
    }

}

2 个答案:

答案 0 :(得分:1)

您可以使用标准Java EE 7 API(Application@ApplicaionPath使用Java代码配置您的REST端点,而不是在web.xml中映射泽西服务器servlet:

@ApplicationPath("api")
public class ApplicationConfig extends Application {
}

这会自动将带有@Path注释的所有资源附加到api后缀。 如果您不希望所有资源都映射到此后缀,则可以覆盖getClasses()方法,特别是列出每个类:

@ApplicationPath("api")
public class ApplicationConfig extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        return Arrays.asList(com.koen.library.BookResource.class);
    }
}

答案 1 :(得分:0)

我的问题的解决方法是:

<param-value>true</param-value>

我必须在我的web.xml中添加该行。 现在我的映射没有任何404错误。

Web.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

     <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>
            org.glassfish.jersey.servlet.ServletContainer
        </servlet-class>
        <init-param>
            <param-name>
                jersey.config.server.provider.packages
            </param-name>
            <param-value>
                com.koen.library
            </param-value>
        </init-param>
        <param-value>true</param-value>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>
</web-app>