JAX-RS Rename Set

Asked

Viewed 47 times

-1

I created a project using JAX-RS to make a web service REST and it works, I can access the result in a resource and everything but it comes like this:

{
  "carroes": {
    "carro": [
      {
        "@id": "1",
        "modelo": "Gol",
        "marca": "VW",
        "ano": 1995
      },
      {
        "@id": "2",
        "modelo": "Golf",
        "marca": "VW",
        "ano": 2010
      },
      {
        "@id": "3",
        "modelo": "Brasilia",
        "marca": "VW",
        "ano": 1984
      },
      {
        "@id": "4",
        "modelo": "Passat Variant",
        "marca": "VW",
        "ano": 1979
      },
      {
        "@id": "5",
        "modelo": "Passat",
        "marca": "VW",
        "ano": 1978
      }
    ]
  }
}

The problem is the name of the set, carroes, should be carros.

The weird thing is I didn’t set it up carroes nowhere, at least I don’t remember putting that name anywhere. Does anyone know where I can change that name?

Look at this service of mine:

@Path("/carros")
@Produces({MediaType.APPLICATION_JSON + ";charset=UTF-8", MediaType.APPLICATION_XML + ";charset=UTF-8"})
@Consumes({MediaType.APPLICATION_JSON + ";charset=UTF-8", MediaType.APPLICATION_XML + ";charset=UTF-8"})
public class CarroService {

    private static CarroDAO carroDao = new CarroDAO();

    @GET        
    public List<Carro> listarCarros(){      
        List<Carro> carrosList = carroDao.findAll();    
        return carrosList;
    }


    @GET
    @Path("{id}")
    public Carro buscaCarroID(@PathParam("id") int id){
        Carro carro = carroDao.findById(id);

        return carro;
    }

    @GET
    @Path("/modelo/{nome}")
    public List<Carro> findCarrosForName(@PathParam("nome") String nome){
        List<Carro> carros = carroDao.findCarrosForName(nome);
        return carros;
    }


    @DELETE
    @Path("{id}")
    public ResponseBuilder deletarCarro(@PathParam("id") int id){
        carroDao.delete(id);

        return Response.ok("Carro Excluido");
    }   
}

1 answer

0

the carrot attribute is probably coming from your "Car" model, when JAX-RS converts (serializes) to JSON it by default uses the same name as its attributes.

Browser other questions tagged

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