可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am facing issues while consuming JAX-RS services as JSON.
Below I have added my code.
This is my service class:
//Sets the path to base URL + /hello @Path("/hello") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public class Hello { @GET @Produces("application/json") public Student getStudent() { Student s = new Student(); s.first_name = "Test First Name !!!"; s.last_name = "Test Last Name!!!"; return s; }
Student
class which I am trying to get from service:
@XmlRootElement public class Student implements Serializable { public String first_name; public String last_name; public String getFirst_name() { return first_name; } public void setFirst_name(String first_name) { this.first_name = first_name; } public String getLast_name() { return last_name; } public void setLast_name(String last_name) { this.last_name = last_name; } public Student() { first_name = "Fahad"; last_name = "Mullaji"; } }
Web XML on service side.
com.vogella.jersey.firstJersey REST Serviceorg.glassfish.jersey.servlet.ServletContainerjersey.config.server.provider.packagescom.vogella.jersey.firstcom.sun.jersey.api.json.POJOMappingFeaturetrue1Jersey REST Service/rest/*
I don't know how to fix this issue. I am using SOAP UI for testing JSON response but I guess that it should not matter.
Many places I read that I need to add the code below. But I don't know where. I am not using Maven for building.
org.glassfish.jersey.mediajersey-media-moxy
Server is looking for function to parse Student
object to JSON but it is not able to find function or jar file for it. I have added jar of Genson, Moxy, RESTEasy and Jackson but I think that's the problem. I think I am missing mapping somewhere.
回答1:
I was able to fix it by install jersey-media-json-jackson
Add the dependency to pom.xml
org.glassfish.jersey.mediajersey-media-json-jacksonruntime
回答2:
You've to create empty constructor because JAX-RS initializes the classes... Your constructor must have no arguments:
@XmlRootElement public class Student implements Serializable { public String first_name; public String last_name; public String getFirst_name() { return first_name; } public void setFirst_name(String first_name) { this.first_name = first_name; } public String getLast_name() { return last_name; } public void setLast_name(String last_name) { this.last_name = last_name; } public Student() { first_name = "Fahad"; last_name = "Mullaji"; } public Student() { } }
回答3:
I was in the same situation where
- I was not using Maven or Ant,
- I finished this Vogella tutorial on Jersey,
- and I was getting the MessageBodyWriter
error when trying to use @Produces(MediaType.APPLICATION_JSON)
.
This answer by @peeskillet solves the problem - you have to use the Jackson *.jar files that are available from the FasterXML Jackson Download page. You'll need the core
files as well as the jaxrs
files.
I added them to my WebContent/WEB-INF/lib
folder where I have my Jersey *.jar files per the above tutorial, and made the small change to the web.xml file below (again, as originally shared by @peeskillet):
jersey.config.server.provider.packages your.other.package.here, com.fasterxml.jackson.jaxrs.json
The important part being com.fasterxml.jackson.jaxrs.json
.
回答4:
Uncommenting the below code helped
org.glassfish.jersey.mediajersey-media-moxy
which was present in pom.xml in my maven based project resolved this error for me.
回答5:
Well, I could not make the above web project working. But I kept on doing research on JAX-RS and I came across Stormpath's tutorial. I think whoever is trying to learn how REST, JSON and Jersey work together should watch this.
Please see links below for their presentation and sample REST project on GitHub .
https://www.youtube.com/watch?v=sVvL12BnIyQ
https://github.com/stormpath/todos-jersey
Now I can compare above project and see what all things I did wrong.
回答6:
Below should be in your pom.xml above other jersy/jackson dependencies. In my case it as below jersy-client dep-cy and i got MessageBodyWriter not found for media type=application/json.
org.glassfish.jersey.mediajersey-media-json-jackson2.25
回答7:
I think may be you should try to convert the data to json format. It can be done by using gson lib. Try something like below.
I don't know it is a best solutions or not but you could do it this way.It worked for me
example : `
@GET @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) public Response info(@HeaderParam("Accept") String accept) { Info information = new Info(); information.setInfo("Calc Application Info"); System.out.println(accept); if (!accept.equals(MediaType.APPLICATION_JSON)) { return Response.status(Status.ACCEPTED).entity(information).build(); } else { Gson jsonConverter = new GsonBuilder().create(); return Response.status(Status.ACCEPTED).entity(jsonConverter.toJson(information)).build(); } }
回答8:
I faced a similar problem because the constructor of this object was using parameters
Before
public MakeMoneyAndGetFitWhileYouSleep(@NotNull UserDetails turkey) {...}
After
public MakeMoneyAndGetFitWhileYouSleep(){} public static MakeMoneyAndGetFitWhileYouSleep getInstance (@NotNull UserDetails turkey) {...}
回答9:
To overcome this issue try the following. Worked for me.
Add following dependency in the pom.xml
org.glassfish.jersey.mediajersey-media-json-jackson2.25
And make sure Model class contains no arg constructor
public Student() { }