You can create @POST method with Jersey returning a List

Asked

Viewed 24 times

2

Good afternoon!

I am developing a Webservice where I need the query method to receive an object as a search parameter and return the list found in the database. I found several tutorials from Webservice Jersey, but nothing with complexity I need.

@POST
    @Path("/Consultar")
    @Consumes(MediaType.APPLICATION_JSON + CHARSET_UTF8)
    @Produces(MediaType.APPLICATION_JSON + CHARSET_UTF8)
    public List<RelacaoCorteVO> Consultar(ConsultaDTO dto) {
        List<RelacaoCorteVO> dtos = null;
        try {
            dtos = getRelacaoCorte().consultar(dto.getCampoConsulta(), dto.getValorConsulta(), dto.getLimite(), dto.getPagina(), true, dto.getNivelMontarDados(), dto.getUsuarioLogado());
            return dtos;
        }catch (Exception e) {
            e.printStackTrace();
            return dtos;
        }
    }
  • good afternoon, all right? what is the return of your method getRelacaoCorte(). consultar()? Another question, wouldn’t it be better to pass as a parameter for this method the Consulted Object dto? So it saves you from sending this bunch of get. would be more or less consult(Consulted dto, Boolean value). Does this method return a list even? As I recall, on its return, Jersey already sends this list (An array of this json object). Hug

  • Good afternoon! Well @Caioaugustopapai

  • So, actually I maintain the system, it wasn’t really well architected, but unfortunately today to make a change like this, I would practically have to rewrite the whole system, so I need webservice to accept an object as a parameter and return a list, I’m gonna have to do this for several classes. The query method returns a List<Relacaocortevo>. As far as I’ve debugged the list and filled in, only Return happens something, the Json List is not created.

  • And what exit are you getting?

  • Oh ta, no error, debugged does not fall in Exeption, but I did some tests, I switched back to Json, and tried to convert the list in JAVA and return Json ready , however Gson can not convert because there are several objects inside each other, but some have attributes with the same name, woe of the error in the conversion.

  • You must use @RequestBody to pass your object to the service, and to return annotate your DTO with @XmlRootElement, If Jersey is set up properly it will handle the rest. If necessary you can envelop your DTO list into a new DTO object and treat it accordingly.

Show 1 more comment

1 answer

0

Thanks to those who answered, the method works as it is, I found that it is glassfish that is intermittent, sometimes works, sometimes not. I have this problem with JDBC connection pool also with it. Container Migrate Solution.

Browser other questions tagged

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