HTTP错误500,用于以RESTful(Jersy 2.x)方法生成XML

时间:2018-10-15 21:39:24

标签: java rest maven jax-rs jersey-2.0

我正在使用Jersey 2.7编写一个简单的RESTfull示例。我正在使用Intelij Idea 2018.2和Tomcat 8.5.34作为集成到它的servlet容器。但是,当我运行该项目时,出现错误500。有人可以帮忙吗?

运行Tomcat并在浏览器中请求以下地址时,浏览器中出现HTTP Status 500 – Internal Server Error。

http://localhost:8080/user/ids/53324

但是当我以调试模式启动Tomcat并请求上述地址时,在浏览器中出现以下异常:

**HTTP Status 500 – Internal Server Error**

Type Exception Report

Message javax.ws.rs.ProcessingException: Last writer interceptor in the chain called the method proceed.

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

javax.servlet.ServletException: javax.ws.rs.ProcessingException: Last writer interceptor in the chain called the method proceed.
    org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:432)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:370)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:389)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:342)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:229)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Root Cause

javax.ws.rs.ProcessingException: Last writer interceptor in the chain called the method proceed.
    org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:159)
    org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:85)
    org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:163)
    org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1135)
    org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:662)
    org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:395)
    org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:385)
    org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:280)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:289)
    org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:256)
    org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:703)
    org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:416)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:370)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:389)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:342)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:229)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

这是我的Maven代码:

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

<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>rest.jersey.jerseyFirst</groupId>
  <artifactId>jerseyPre</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <properties>
     <jersey.version>2.27</jersey.version>
  </properties>

  <dependencies>

    <dependency>
      <groupId>org.glassfish.jersey.containers</groupId>
      <artifactId>jersey-container-servlet</artifactId>
      <version>${jersey.version}</version>
    </dependency>

    <dependency>
      <groupId>org.glassfish.jersey.core</groupId>
      <artifactId>jersey-client</artifactId>
      <version>${jersey.version}</version>
    </dependency>
    <dependency>
      <groupId>org.glassfish.jersey.inject</groupId>
      <artifactId>jersey-hk2</artifactId>
      <version>${jersey.version}</version>
    </dependency>

    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>6.0</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>javax.xml.bind</groupId>
      <artifactId>jaxb-api</artifactId>
      <version>2.3.0</version>
    </dependency>
    <dependency>
      <groupId>javax.activation</groupId>
      <artifactId>activation</artifactId>
      <version>1.1</version>
    </dependency>

  </dependencies>

</project>

这是我的Java类:

package rest.jersey.jerseyFirst;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Path("/user")
public class HelloWorld {

@GET
@Path("/hello")
@Produces(MediaType.TEXT_PLAIN)
public String getMessage() {
    return "Hello world !!!";
}

@GET
@Path("/ids/{id}")
@Produces(MediaType.APPLICATION_XML)
public User getUserInXML(@PathParam("id") int id) {
    User user = new User();
    user.setName("Kia");
    user.setId(id);
    return user;
}

}

这是用户类:

package rest.jersey.jerseyFirst;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "user")
public class User {
    private String name;
    private int id;

    public User(){

    }

    public User (String name, int id){
        this.id = id;
        this.name = name;
    }

    @XmlElement
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlElement
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

这是我的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_4_0.xsd"
         version="4.0">
  <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>rest.jersey.jerseyFirst</param-value>
    </init-param>

    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

</web-app>

预先感谢

0 个答案:

没有答案