简体   繁体   English

宁静的JAX-RS服务器错误

[英]Restful JAX-RS Server Error

I have problems using Restful Webservices using the Jersey implementation. 我在使用Jersey实现方式使用Restful Web服务时遇到问题。 In the documentation it is written that i should download this : Jersey JAX-RS 2.0 RI bundle , unzip it and add the jars to my WEB-INF/lib folder. 在文档中写道,我应该下载此文件: Jersey JAX-RS 2.0 RI软件包 ,将其解压缩并将其添加到我的WEB-INF / lib文件夹中。 These jars are: 这些罐子是:

  • jersey-client 球衣的客户端
  • jersey-common 球衣常见
  • jersey-container-servlet 新泽西州容器的servlet
  • jersey-container-servlet-core 新泽西州容器servlet的核心
  • jersey-server 球衣服务器

If i do so i always get the following exception: 如果我这样做,我总是会遇到以下异常:

java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer

After some researching i found out that i should add jersey-servlet-1.12.jar instead of adding the jars from the download source posted above. 经过一些研究,我发现我应该添加jersey-servlet-1.12.jar而不是从上面发布的下载源中添加jar。 So I did it. 所以我做到了。 I added this to my web.xml 我将此添加到我的web.xml

<servlet>
    <servlet-name>JAX-RS Servlet</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>JAX-RS Servlet</servlet-name>
    <url-pattern>/jaxrs/*</url-pattern>
</servlet-mapping>

And i implemented a small test-class: 我实现了一个小型测试类:

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import de.hof.university.spj.model.Movie;

@Path("/test")
public class Test {

    @GET
    @Produces("application/json; charset=UTF-8")
    public List<Movie> getMovieList() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("SPJUnit");
        EntityManager em = factory.createEntityManager();

        TypedQuery<Movie> query = em.createQuery("Select m FROM Movie m WHERE m.movieID = 4204", Movie.class);

        return query.getResultList();
    }
}

When i now call http://localhost:8080/MyApp/jaxrs/test i get this error: 当我现在致电http://localhost:8080/MyApp/jaxrs/test此错误:

HTTP Status 500 - Internal Server Error. The server encountered an internal error (Internal Server Error) that prevented it from fulfilling this request.

I dont know how to do use the jersey implementation correctly. 我不知道如何正确使用Jersey实现。

Why don't you just follow the Getting Started guide? 您为什么不只遵循《 入门指南》? It tells you exactly how to go about this. 它确切地告诉您如何进行此操作。

You'd see that you need, amongst a few others, these JARs in WEB-INF/lib : 您会发现,除其他外,您还需要WEB-INF/lib这些JAR:

javax.annotation-api-1.2.jar
javax.inject-2.1.88.jar
javax.ws.rs-api-2.0.jar
jersey-client-2.0.jar
jersey-common-2.0.jar
jersey-container-servlet-core-2.0.jar
jersey-server-2.0.jar

You'd also see that the web.xml needs to look similar to this: 您还将看到web.xml需要看起来像这样:

<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.example</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/webapi/*</url-pattern>
</servlet-mapping>

I solved the Problem. 我解决了问题。

I deleted all jersey jars and i deleted the webservices part from the web.xml. 我删除了所有球衣瓶,并从web.xml中删除了webservices部分。

I changed my test class to this: 我将测试类更改为:

import java.io.Serializable;
import java.util.List;

import javax.enterprise.context.RequestScoped;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
import de.hof.university.spj.model.Movie;

@RequestScoped
@ApplicationPath("/webservices")
@Path("/movies")
public class Test extends Application implements Serializable {
    private static final long serialVersionUID = 1L;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Movie> getMovieList() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("SPJUnit");
        EntityManager em = factory.createEntityManager();

        TypedQuery<Movie> query = em.createQuery("Select m FROM Movie m WHERE m.movieID = 4204", Movie.class);

        return query.getResultList();
    }
}

And i added @XmlRootElement to my Movie Entity class. 我将@XmlRootElement添加到我的电影实体类中。

Now it works fine 现在工作正常

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

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