yeahservice
yeahservice

Reputation: 37

JAX-RS POJO to json does not work with payara 5

I have the following entity:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Recipe {

    private String title;
    private int preparationTime;

    public Recipe() {
    }

    public Recipe(String title, int preparationTime) {
        this.title = title;
        this.preparationTime = preparationTime;
    } 
}

and resource:

@Path("recipes")
@Stateless
public class RecipesResource {

    @GET
    public Recipe getRecipe() {
        return new Recipe("cake", 120);
    }
}

Requesting application/xml works fine and I get an xml object returned. Requesting application/json instead gives me an empty json object. If I add getter/setter to the Recipe class it also works for json.

My old setup with java-ee 7 and payara 4 worked fine without the getter/setters. I only changed to java-ee 8 and payara 5 and it does not work anymore, am I missing some configuration? Shouldn't @XmlAccessorType(XmlAccessType.FIELD) remove the necessity of getters/setters?

Upvotes: 1

Views: 930

Answers (1)

Ondro Mihályi
Ondro Mihályi

Reputation: 7710

You need to add JSON-B annotations to the Recipe class. This is the preferred standard way to map Java classes to JSONin Java EE 8.

In Java EE 7, there was no standard way how to map Java objects to JSON. Some application servers, including Payara 4, can translate JAX-B annotations into JSON. But it's not standard and doesn't work all the time because annotations like @XmlRootElement are meant to map Java objects to XML and not JSON.

Payara 5 uses the new JSON-B API to map to JSON and ignores XML annotations.

Upvotes: 3

Related Questions