Request without @XmlRootElement

0

I'm doing a simple user registration with Java and AngularJS.

My request in javascript looks like this:

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

In Java, I have a method that gets 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 the statement of my UserPojo , I'm being forced to put the @XmlRootElement annotation to work 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 would like to know how to do without @XmlRootElement , as I'm not sending anything with XML.

    
asked by anonymous 21.12.2016 / 16:15

1 answer

1
  

TL / DR : Keep JAXB annotations if you have no greater reason to leave the default. If you really need something specific, enable Jackson.

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

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

If JAXB does not really meet the needs of your application, you can also use Jackson directly. Among other things, Jackson manages to work with unmarked POJOs. There are some corner cases where Jackson may be worth it, for example when you need fine settings not available in MOXy by serializing / deserializing JSON.

To enable Jackson support, two things are needed:

  • Your application needs to reference the correct dependencies to make Jersey work with Jackson (see Maven settings or the list of jars needed to make Jersey 2.25 work with Jackson 2.8.4).

  • You need to set up Jersey to use Jackson

    final Application application = new ResourceConfig()
        .packages("com.minhaempresa.meuprojeto.meuspojos")
        .register(JacksonFeature.class); // habilita o Jackson
    
  • For more details see the documentation on Jackson's use with Jersey

    With this configuration, Jersey will use Jackson's ObjectMapper to serialize and deserialize POJOs. Jackson is able to serialize POJOs without being annotated with any annotations, but supports self annotations as well as JAXB annotations when they are needed.

    See Jackson JAX-RS JSON Provider Example in Jersey's Git repository for one a complete example with features that exposes POJOs not annotated, as well as annotated with Jackson and JAXB.

        
    21.12.2016 / 17:57