Request without @Xmlrootelement

Asked

Viewed 589 times

0

I’m doing a simple user registration with Java and Angularjs.

My javascript request looks like this:

$http({
    url: "rest/user/register",
    method: "POST",
    data: $scope.newUser
});

In java, I have a method that receives the data in a POJO.

@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
@Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8")
@POST
@Path("/register")
public String register(UserPojo userPojo){  
    return "teste";
}

The problem I’m facing is that in my statement UserPojo, I’m being forced to put the note @XmlRootElement so that it works normally.

If I take the note, the following errors occur:

GRAVE: A message body reader for Java class br.com.taskmanagement.pojo.UserPojo, 
and Java type class br.com.taskmanagement.pojo.UserPojo, 
and MIME media type application/json; charset=UTF-8 was not found.

The registered message body readers compatible with the MIME media type are:
application/json; charset=UTF-8 ->

I’d like to know how to do without @XmlRootElement, because I’m not sending anything with XML.

  • Using some lib from Json? Ex: jersey-media-json-jackson

  • @I’m wearing jersey-json.jar

  • Try using this to test for the error some: https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-json-jackson/2.25

  • I’m not familiar with jersey-json, but you may need to use the @Xmlrootelement annotation with it.

  • @Diegoaugusto I tried with the lib you commented, but it didn’t work either :(

  • Do you use Maven? If yes you can add its dependencies?

  • @Diegoaugusto I’m not using Maven, all in hand.

  • Get it, get it. http://stackoverflow.com/questions/12179737/jersey-json-media-type-application-json-was-not-found

Show 3 more comments

1 answer

1


TL/DR: Keep JAXB’s notes if you have no greater reason to break the pattern. If you really need something specific enable the Jackson.

In current versions of Jersey to standard form to work with JSON is using Moxy. Also by default current distributions use annotations from JAXB (like the @XmlRootElement) for both XML and JSON.

While JAXB was originally designed to work with XML (this is why annotations are specific to XML), there is nothing wrong with using JAXB annotations to work with JSON. The great advantage of working with JAXB, in addition to saving you the trouble of modifying Jersey settings, is that all your mapping can be repurposed in the future if your application ever needs XML support.

If JAXB really doesn’t meet the requirements of your application, you can also use Jackson directly. Among other things Jackson manages to work with annotated Pojos. There are some corner cases where Jackson might be worth it, for example when you need fine settings not available in Moxy when serializing / deserializar JSON.

Two things are needed to enable Jackson support:

  1. Your app needs to reference the right dependencies to make Jersey work with Jackson (see the settings of Maven or the list of necessary jars to make Jersey 2.25 work with Jackson 2.8.4).

  2. You need to configure Jersey to use Jackson

    final Application application = new ResourceConfig()
        .packages("com.minhaempresa.meuprojeto.meuspojos")
        .register(JacksonFeature.class); // habilita o Jackson
    

For more details see documentation on Jackson’s use with Jersey

With this configuration Jersey starts to use the ObjectMapper Jackson to serialize and deserialize Pojos. Jackson is able to serialize Pojos without them being marked with any annotation, but supports own notes as well as JAXB annotations when these are necessary.

Behold Jackson JAX-RS JSON Provider Example in the Jersey Git repository for a full-featured example of annotated Pojos, as well as annotated with Jackson and JAXB.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.