Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

eclipse - Jersey: A message body writer for Java Class and MIME mediatype application/json was not found

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;


    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...