javax.ws.rs.WebApplicationException: com.sun.jerse

2019-06-18 10:09发布

Okay, this question has probably been asked before, but on all sites I've looked the explanation on "how to do this" tells me I'm doing it completely right.

I know I'm not, as I get a 500 server error on my localhost tomcat and I get the following error in my server logs:

javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class com.myapp.domain.Location, and Java type class com.myapp.domain.Location, and MIME media type application/json was not found

So, what I'm trying to do is to develop a RESTful web service with Jersey (in Java). Everything is going fine, except for the fact that I want to return JSON. I can't find what I'm doing different from these people:

My POJO (Location) looks like this:

package com.myapp.domain;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement()
public class Location {
    private int id;
    private double longtitude;
    private double latitude;

    public Location() {
        new Location(-1, -1, -1);
    }

    public Location(double longtitude, double latitude) {
        new Location(-1, longtitude, latitude);
    }

    public Location(int id, double longtitude, double latitude) {
        this.id = id;
        this.longtitude = longtitude;
        this.latitude = latitude;
    }

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

    public void setLongtitude(double longtitude) {
        this.longtitude = longtitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public int getID() {
        return this.id;
    }

    public double getLongtitude() {
        return this.longtitude;
    }

    public double getLatitude() {
        return this.latitude;
    }
}

My resource looks like this:

package com.myapp.MyAPP;

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

import com.myapp.domain.Location;

@Path("Locations")
public class LocationInfo {
    @GET
    @Path("/get/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Location getLocation(@PathParam("id") int id) {
        Location loc = new Location(3, 4.007391, 51.00237);
        return loc;
    }
}

And this is my web.xml:

<?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" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        id="WebApp_ID" 
        version="2.5">  
    <display-name>MyAPP</display-name> 
    <servlet> 
            <servlet-name>MyAPP REST Service</servlet-name> 
            <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 
            <init-param> 
                <param-name>com.sun.jersey.config.property.packages</param-name> 
                <param-value>com.myapp.MyAPP</param-value>
            </init-param>
            <init-param>
                <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
                <param-value>true</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
            <servlet-name>MyAPP REST Service</servlet-name> 
            <url-pattern>/rest/*</url-pattern> 
    </servlet-mapping>
</web-app>

I've got these libraries included: asm-3.1.jar, jersey-client-1.17.1.jar, jersey-core-1.17.1.jar, jersey-json-1.17.1.jar, jersey-server-1.17.1.jar, jersey-servlet-1.17.jar, jsr11-api-1.1.1.jar

The one who sees what I'm not seeing gets a beer. Or at least my eternal gratitude, cause I've been looking at this for way too long and I still can't see it.

1条回答
手持菜刀,她持情操
2楼-- · 2019-06-18 10:37

Okay, so I turned out orid had the right answer: I simply needed to add some extra libraries!

I probably overlooked it or most tutorials probably suppose you add all the libraries you download with jersey straight away...

So this fixed the problem: adding

  • jackson-core-asl-1.9.2.jar
  • jackson-jaxrs-1.9.2.jar
  • jackson-mapper-asl-1.9.2.jar
  • jackson-xc-1.9.2.jar

Thanks again to orid, you just saved my weekend.

查看更多
登录 后发表回答