简体   繁体   English

javax.ws.rs.WebApplicationException:com.sun.jersey.api.MessageException:Java REST Web服务中的JSON支持

[英]javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: JSON support in Java REST Webservices with Jersey

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: 我知道我不是,因为我的localhost tomcat上出现500服务器错误,我的服务器日志中出现以下错误:

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). 因此,我要做的是使用Jersey(Java)开发RESTful Web服务。 Everything is going fine, except for the fact that I want to return JSON. 一切都很顺利,除了我想要返回JSON的事实。 I can't find what I'm doing different from these people: 我无法找到与这些人不同的东西:

My POJO (Location) looks like this: 我的POJO(位置)看起来像这样:

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: 这是我的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 我有这些库包括: asm-3.1.jarjersey-client-1.17.1.jarjersey-core-1.17.1.jarjersey-json-1.17.1.jarjersey-server-1.17.1.jarjersey-servlet-1.17.jarjsr11-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. 或者至少是我永恒的感激之情,因为我一直在看这个太久了,我仍然看不到它。

Okay, so I turned out orid had the right answer: I simply needed to add some extra libraries! 好吧,所以我发现orid有正确的答案:我只需要添加一些额外的库!

I probably overlooked it or most tutorials probably suppose you add all the libraries you download with jersey straight away... 我可能忽略了它,或者大多数教程可能会假设你直接添加你用jersey下载的所有库...

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. 再次感谢orid,你刚刚结束了我的周末。

暂无
暂无

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

相关问题 在宁静的Web服务应用程序中获取com.sun.jersey.api.MessageException - Getting com.sun.jersey.api.MessageException in restful webservices application 引发WebApplicationException时出错:com.sun.jersey.api.MessageException:找不到Java类java.util.HashMap的消息正文编写器 - error while throwing WebApplicationException : com.sun.jersey.api.MessageException: A message body writer for Java class java.util.HashMap not found 查询 Jersey REST 服务时的消息传递异常 (com.sun.jersey.api.MessageException) - Messaging exception (com.sun.jersey.api.MessageException) while querying Jersey REST service 显示异常javax.ws.rs.WebApplicationException的jersey休息服务:javax.xml.bind.MarshalException - jersey rest services showing exception javax.ws.rs.WebApplicationException: javax.xml.bind.MarshalException javax.ws.rs.WebApplicationException而不是Jersey Exception Mapper中的实际异常 - javax.ws.rs.WebApplicationException instead of the actual exception in Jersey Exception Mapper 骑行服api com.sun.jersey.api.container.ContainerExceptionServlet - Jersey rest api com.sun.jersey.api.container.ContainerExceptionServlet java.lang.ClassCastException:无法将com.sun.jersey.core.impl.provider.entity.StringProvider转换为javax.ws.rs.ext.MessageBodyReader - java.lang.ClassCastException: Cannot cast com.sun.jersey.core.impl.provider.entity.StringProvider to javax.ws.rs.ext.MessageBodyReader javax.ws.rs.NotSupportedException:找不到以下类型的消息正文阅读器:com.sun.jersey.core.header.FormDataContentDisposition类 - javax.ws.rs.NotSupportedException: Could not find message body reader for type: class com.sun.jersey.core.header.FormDataContentDisposition java.lang.ClassNotFoundException:com.sun.jersey.core.spi.factory.ResponseImpl REST-FUL Webservices - java.lang.ClassNotFoundException: com.sun.jersey.core.spi.factory.ResponseImpl REST-FUL Webservices javax.ws.rs.WebApplicationException:HTTP 404服务器找不到请求的资源 - javax.ws.rs.WebApplicationException: HTTP 404 the server could not find the requested resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM