泽西岛:找不到 Java 类和 MIME 媒体类型应用程序/json 的消息正文编写器 [英] Jersey: A message body writer for Java Class and MIME mediatype application/json was not found

查看:38
本文介绍了泽西岛:找不到 Java 类和 MIME 媒体类型应用程序/json 的消息正文编写器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在试图弄清楚我的问题是什么之后,我终于决定问你如何解决我的问题.我见过不同的人有同样的问题,我尝试了他们被建议做的所有事情,但对我的问题没有任何帮助.所以基本上我有一个使用 Jersey 构建的 RESTful 服务.对于我的客户,我想以 JSON 格式返回一个对象.我阅读了不同的教程并决定使用 jersey-json-1.8 库是有意义的.我像往常一样将所有内容添加到我的项目中并尝试运行它,但每次我调用服务时(通过 get request atm.)我收到 HTTP 错误代码 500(内部服务器错误)并且我的服务器响应没有消息正文作家可以找到.如果我返回 XML,它工作得很好,一切都很好.我还尝试将 jersey-json-1.8.jar 复制到我的 Tomcat lib 文件夹,因为我必须使用我正在使用的 mysql lib 执行此操作,但它也没有帮助.如果你能帮我把这些东西弄好,我会很高兴的!如果您需要更多信息,请发表评论,我会尽快提供:)

after trying to figure out what's my problem I finally decided to ask you how to solve my problem. I've seen different people with the same problem and I tried all the things they were adviced to do but nothing helped with my issue. So basically I'm having a RESTful Service which I build using Jersey. For my client I would like to return an object in JSON Format. I read through different tutorials and decided that it makes sense to use jersey-json-1.8 library. I added everything to my project as usual and tried to run it but each time I'm calling the service (via get request atm.) I'm getting HTTP Error Code 500 (internal server error) and my server responds that no message body writer could be found. If I'm returning XML it works just fine and everything is great. I also tried copying jersey-json-1.8.jar to my Tomcat lib folder because I had to do this with the mysql lib I'm using but it didn't help either. I would be really glad if you could help me out to get this stuff working! If you need any more information just leave a comment and I'll provide it as quickly as humanly possibly :)

我的项目设置是:3 不同的包装1. 我的 RESTfulServices2. 我的 Java 工作,我处理 SQL 连接等.3. 一个包,其中存储了我需要使用的所有模型,并且我想以 JSON 格式返回(在我的示例中为试驾路线)

My project setup is: 3 Different packages 1. My RESTfulServices 2. My Java Work where i handle SQL connections etc. 3. A package where I store all my models that I need to work with and that I want to return in JSON Format (in my example a route for a testdrive)

Tomcat 网络服务器IDE:日食我正在使用 Maven

A Tomcat Webserver IDE: Eclipse I'm using Maven

无论我以什么方式或如何尝试返回对象,它都无法正常工作,并且我不断收到错误消息:

No matter what or how I'm trying to return the Object it just won't work and I'm constantly getting the error message :

Mapped exception to response: 500 (Internal Server Error)
javax.ws.rs.WebApplicationException: com.sun.jersey.api.MessageException: A message body writer for Java class com.mykong.models.Teststrecke, and Java type class com.mykong.models.Teststrecke, and MIME media type application/json was not found

这是我的 JSON 服务方法

Here's my JSON Service Method

@Path("/hellojson")
public class JSONService {


    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public ArrayList<Route> getJSONMsg()  
    {

        Route ts = new Route();
        ts.setId(1);
        ts.setName("HelloWorld");


        Route ts2 = new Route();
        ts2.setId(2);
        ts2.setName("HelloWorld");


        ArrayList<Route> availRoutes = new ArrayList<Route>();
        availRoutes.add(ts);
        availRoutes.add(ts2);


        return availRoutes;


    }
}

推荐答案

尝试将 Genson 库添加到你的类路径 http://owlike.github.io/genson/.它是一个 json<>java 流和数据绑定 api.它与球衣很好地集成在一起,要让您运行,您需要 0 配置.Jersey 检测到该库在您的类路径中,并通过委托给 Genson 来启用 json 映射.

Try adding Genson library to your classpath http://owlike.github.io/genson/. It is a json<>java streaming and databinding api. It integrates well with jersey, to get you running you need 0 configuration. Jersey detects that the library is in your classpath and enables json mapping by delegating to Genson.

这篇关于泽西岛:找不到 Java 类和 MIME 媒体类型应用程序/json 的消息正文编写器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆