简体   繁体   中英

JAX-RS Jersey JSON GET HTTP Status 500 Internal Server Error

I'm learning JAX-RS and I'm getting a HTTP Status 500 Internal Server Error when making a GET request which produces JSON. XML works fine.

I've searched other questions but can't find a solution. This may be down to my current lack of knowledge. However, from what I have read there is a suggestion that my project is missing a JAR in the build path.

I'm not using Maven. I've just created a dynamic web app in Eclipse and added the JAX-RS / Jersey jars to my WEB-INF/lib directory and added them to my build path.

I'm using Postman to test the web service. I'm adding Accept to the header when making the requests:

Accept : application/xml - works fine. Accept : application/json - Internal Server Error

Here's my (very simple code) code:

@XmlRootElement
public class Thing {

    private String name;

    public Thing(){}

    public String getName() {
        return name;
    }

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

}

The service is set up to produce both XML and JSON.

@Path("/get")
public class Service {

    @GET
    @Path("/both")
    @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
    public Thing getThingResponses(){

        Thing t = new Thing();
        t.setName("I'm a thing!");
        return t;

    }


}

Any help / pointers will be appreciated. Thanks

You probably forgot to add all the needed dependencies you need to convert from java to json. Assuming that you are using maven, here is what you need(the versions are maybe not the last ones, check it out):

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.webapp</groupId>
  <artifactId>restservice</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>restservice Maven Webapp</name>
  <url>http://maven.apache.org</url>


  <dependencies>

    <dependency>
      <groupId>asm</groupId>
      <artifactId>asm</artifactId>
      <version>3.3.1</version>
    </dependency>
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-bundle</artifactId>
      <version>1.19.3</version>
    </dependency>
    <dependency>
      <groupId>org.json</groupId>
      <artifactId>json</artifactId>
      <version>20140107</version>
    </dependency>
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-server</artifactId>
      <version>1.19.3</version>
    </dependency>
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-core</artifactId>
      <version>1.19.3</version>
    </dependency>

  </dependencies>
  <build>
    <finalName>restservice</finalName>
  </build>
</project>

Then here you have the web.xml that should load the classes where your rest is defined:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>com.vogella.jersey.first</display-name>
    <servlet>
        <servlet-name>jersey-servlet</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>org.restservice</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>jersey-servlet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

and finally a bunch of methods like yours:

package org.restservice;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 * Created by paolo on 06/05/17.
 */
@Path("/hello")
public class Hello {

    // This method is called if TEXT_PLAIN is request
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayPlainTextHello() {
        return "Hello Jersey";
    }

    // This method is called if XML or JSON is request
    @GET
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Thing sayXMLorJSONHello() {
        Thing t = new Thing();
        t.setName("I'm a thing!");
        return t;
    }

    // This method is called if HTML is request
    @GET
    @Produces(MediaType.TEXT_HTML)
    public String sayHtmlHello() {
        return "<html> " + "<title>" + "Hello Jersey" + "</title>"
                + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";

    }

}

I tested it on a tomcat 9 and the URL I used was:

localhost:8080/rest/hello with header:

accept: application/json
accept-encoding: gzip, deflate
accept-language: en-US,en;q=0.8
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36

Here what I get:

{
"name": "I'm a thing!"
}

Good luck.

UPDATE: OK you're not using maven, well add the same jars in the lib folder of your project... or start using maven ;-)

Can you check if the library is copied to the server? I don't know which server you are using

On the other hand, can you show as the trace of the error?

To resolve this issue I built a Maven web app as Paolof76 suggested.

However, I used Jersey 2 so I was unable to follow the example directly.

The following dependencies were required to get the JSON MediaType to work:

    <!-- https://mvnrepository.com/artifact/org.json/json -->
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20160810</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-json-jackson -->
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.0-m07</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.jaxrs/jackson-jaxrs-json-provider -->
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.9.0.pr3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-moxy -->
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
        <version>2.26-b03</version>
    </dependency>

There was a bit of trial and error involved but I got there in the end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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