i have implemented rest webservices using Jersey, and whenever some exception occur on the server side, the client gets a generic HTTP 500 Internal Server Error, with no more info of the real exception. I found that people usually catch any exception on the server side, then throws a WebApplicationException, but even this way the client keeps getting the generic HTTP 500 Internal Server Error.
This is my webservice:
@PUT @Produces(MediaType.APPLICATION_XML) @Consumes(MediaType.APPLICATION_XML) @Path("/transmitir") public WrapperTransmissaoRetorno receber(WrapperTransmissao wrapperRecepcao) { WrapperTransmissaoRetorno retorno = new WrapperTransmissaoRetorno(); retorno.setCodigoMaster(new Random().nextInt()); retorno.setDataRetorno(new Date()); if(true){ throw new WebApplicationException("Este pau eh bem graudo"); } return retorno; } This is the code that calls the client:
try { WsTransmissaoCliente client = new WsTransmissaoCliente(); WrapperTransmissao wrapperRecepcao = new WrapperTransmissao(); Transferencia transferencia = new Transferencia(); transferencia.setCodigoTabela(23); transferencia.setCodigoTransferencia(56); transferencia.setDataRetorno(new Date()); transferencia.setDataTransmissao(new Date(System.currentTimeMillis()+3000000)); transferencia.setNomeTabela("CUPOM"); transferencia.setTipoOperacao(TipoOperacao.UPDATE); wrapperRecepcao.setTransferencia(transferencia); Jumento jumento = new Jumento(); jumento.setIdade(24); jumento.setNome("José"); wrapperRecepcao.setObjeto(jumento); // Cabrito cabrito = new Cabrito(); // cabrito.setAltura(56); // cabrito.setPeso(120.0); // wrapperRecepcao.setObjeto(cabrito); WrapperTransmissaoRetorno retorno = client.transmitir(wrapperRecepcao); System.out.println("Retorno do WS: "+retorno); } catch (Exception e) { WebApplicationException exx = (WebApplicationException) e; exx.printStackTrace(); } How to avoid this and get the real exception? Or at least the message?
UPDATE Here is the object i am sending as a response:
package br.atualy.integracaocheckout.wrappers; import java.util.Date; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class WrapperTransmissaoRetorno { private Date dataRetorno; private Integer codigoMaster; public Date getDataRetorno() { return dataRetorno; } public void setDataRetorno(Date dataRetorno) { this.dataRetorno = dataRetorno; } public Integer getCodigoMaster() { return codigoMaster; } public void setCodigoMaster(Integer codigoMaster) { this.codigoMaster = codigoMaster; } @Override public String toString() { return "WrapperRecepcaoRetorno{" + "dataRetorno=" + dataRetorno + ", codigoMaster=" + codigoMaster + '}'; } } UPDATE 2 And here is the client:
import br.atualy.integracaocheckout.wrappers.WrapperTransmissao; import br.atualy.integracaocheckout.wrappers.WrapperTransmissaoRetorno; import javax.ws.rs.ClientErrorException; import javax.ws.rs.client.Client; import javax.ws.rs.client.WebTarget; public class WsTransmissaoCliente { private final WebTarget webTarget; private final Client client; private static final String BASE_URI = "http://localhost:8080/IntegracaoCheckout/webresources"; public WsTransmissaoCliente() { client = javax.ws.rs.client.ClientBuilder.newClient(); webTarget = client.target(BASE_URI).path("transmissao"); } // public String receber() throws ClientErrorException { // WebTarget resource = webTarget; // resource = resource.path("receber"); // return resource.request(javax.ws.rs.core.MediaType.APPLICATION_XML).get(String.class); // } public WrapperTransmissaoRetorno transmitir(WrapperTransmissao requestEntity) throws ClientErrorException { return webTarget.path("transmitir") .request(javax.ws.rs.core.MediaType.APPLICATION_XML) .put(javax.ws.rs.client.Entity.entity(requestEntity, javax.ws.rs.core.MediaType.APPLICATION_XML), WrapperTransmissaoRetorno.class); } public void close() { client.close(); } }