Rest only returns XML

Asked

Viewed 101 times

0

good evening to all,

I’m making a Restfull API for mere learning and ran into a simple problem, but I couldn’t find a solution on the internet.

I created this class to control games:

@Path("/games")
public class ManipulaGameRest {

@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public List getListaGamesCadastrados(){
      return ControlaGame.getListaGames();
}

when testing in Postman it worked perfectly, at first glance, however when trying to get WS to return JSON, passing "Content-Type" as "application/json" it still returned me an XML...

all I found on the net was to put only the "Mediatype.APPLICATION_JSON", as it did not work I was left without knowing what to do.

Can someone tell me how to solve this problem???

NOTE: no error occurred on console.

1 answer

1


I found an answer at Stackoverflow International that you have to write your class down as follows:

@XmlRootElement
@GET @Produces("application/json")
@XmlRootElement
  public class MyJaxbBean {
    public String name;
    public int age;

    public MyJaxbBean() {} // JAXB needs this

    public MyJaxbBean(String name, int age) {
      this.name = name;
      this.age = age;
    }
  }
and then your method would look like this:

   @GET @Produces("application/json")
   public MyJaxbBean getMyBean() {
      return new MyJaxbBean("Agamemnon", 32);
   }
There is a chapter in the latest documentation that deals with this:

https://jersey.java.net/documentation/latest/user-guide.html#json

Following link: https://stackoverflow.com/questions/13594945/how-correctly-produce-json-by-restful-web-service

  • had already done it, had not solved

Browser other questions tagged

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