0

boa noite a todos,

estou fazendo um uma API RestFull pra mero aprendizado e esbarrei em um problema simples, mas não achei solução na internet.

criei essa classe pra fazer o controle de games:

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

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

ao testar no postman ela funcionou perfeitamente, a primeira vista, porem ao tentar que o WS retornasse JSON, passando “Content-Type” como “application/json” ele ainda me retornou um XML…

tudo o que eu achei na net era pra colocar apenas o “MediaType.APPLICATION_JSON”, como não funcionou eu fiquei sem saber o q fazer.

alguem sabe me dizer como resolvo esse problema???

OBS: não ocorreu nenhum erro no console.

1 Resposta 1

1

Achei uma resposta no Stackoverflow internacional que você tem que anotar tua classe da seguinte forma:

@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

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

1
  • ja tinha feito isso, não havia solucionado 25/02/2019 às 1:12

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .