泽西:没有找到Java类和MIME媒体类型application / json的消息体编写器 [英] Jersey: A message body writer for Java Class and MIME mediatype application/json was not found

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

问题描述

试图找出我的问题后,我终于决定问你如何解决我的问题。我看到不同的人有同样的问题,我尝试了所有的建议,但没有任何帮助我的问题。所以基本上我有一个RESTful服务,我建立使用泽西。对于我的客户端,我想以JSON格式返回一个对象。我阅读了不同的教程,并决定使用jersey-json-1.8库是有意义的。我像往常一样将所有东西添加到我的项目中,并尝试运行它,但每次调用服务(通过get request atm)。我得到HTTP错误代码500(内部服务器错误),我的服务器响应没有消息体作家可以找到。
如果我正在返回XML,它的工作原理很好,一切都很好。我也尝试将jersey-json-1.8.jar复制到我的Tomcat lib文件夹,因为我不得不使用我正在使用的mysql lib,但它也没有帮助。
如果您能帮助我找到这些东西,我真的很高兴!
如果您需要任何更多信息,只需发表评论,我将尽可能快地提供:)



我的项目设置是:
3不同的包
1.我的RESTfulServices
2.我的Java工作,我处理SQL连接等
3.一个包,我存储我需要处理的所有模型,我想以JSON格式返回(在我的示例中为testdrive的路由)



Tomcat Webserver
IDE:Eclipse
我正在使用Maven



无论什么或如何我要返回对象,它将无法正常工作,我不断收到错误消息:

 映射异常到响应:500(内部服务器错误)
javax.ws.rs.WebApplicationException:com.sun.jersey.api。 MessageException:Java类com.mykong.models.Teststrecke和Java类型类com.mykong.models.Teststrecke和MIME媒体类型application / json的消息体编写器未找到

编辑:
这是我的JSON服务方法

  @Path(/ hellojson)
public class JSONService {


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

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


路由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配置。泽西检测到库位于您的类路径中,并通过委派Genson来启用json映射。


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 :)

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)

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

EDIT: 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;


    }
}

解决方案

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媒体类型application / json的消息体编写器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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