Bom dia, Fiz o seguinte método na classe CarrinhoResource. Estou testando a retorno tanto de XML como de JSON.
@Path("{id}")
@GET
@Produces(MediaType.APPLICATION_JSON)
//@Produces(MediaType.APPLICATION_XML)
public Carrinho busca(@PathParam("id") long id) {
Carrinho carrinho = new CarrinhoDAO().busca(id);
return carrinho;
}
Utilizo o seguinte teste:
@Test
public void testaQueBuscarUmCarrinhoTrazOCarrinhoEsperado() {
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080");
//--
Carrinho carrinho = target.path("/carrinhos/1").request().get(Carrinho.class);
//--
System.out.println(" RETORNO :"+carrinho.getRua());
Assert.assertEquals("Rua Vergueiro 3185, 8 andar", carrinho.getRua());
}
Cenário 1:(para retorno em XML)
-Quando habilito o retorno(Produces) para XML funciona perfeitamente.
Cenário 2:(para retorno em JSON)
-Quando habilito o retorno(Produces) para JSON retorna o seguinte erro:
javax.ws.rs.InternalServerErrorException: HTTP 500 MessageBodyWriter not found for media type=application/json, type=class br.com.alura.loja.modelo.Carrinho, genericType=class br.com.alura.loja.modelo.Carrinho.
at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:929)
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:770)
at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:90)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:671)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:422)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:667)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:396)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:296)
at br.com.alura.loja.ClienteTest.testaQueBuscarUmCarrinhoTrazOCarrinhoEsperado(ClienteTest.java:88)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)