Jersey 2 library response.getEntity does not exist, what should I use instead

my code

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package beans; import clients.NewJerseyClient; import entities.ReservationItem; import java.io.Serializable; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.ws.rs.core.GenericType; import parameters.ReservationParam; import org.glassfish.jersey.client.ClientResponse; /** * * @author subhi2 */ @ManagedBean @SessionScoped public class PageController implements Serializable { public String moveToPage2() { NewJerseyClient client = new NewJerseyClient(); ClientResponse response = client.findInsertedReservationItem(ClientResponse.class, "22", "2010-07-26T11:15:51", "2014-07-26T11:15:51"); GenericType<List<ReservationItem>> genericType = new GenericType<List<ReservationItem>>() { }; // Returns an ArrayList of Players from the web service List<ReservationItem> data = new ArrayList<ReservationItem>(); data = (response.getEntity(genericType)); return data.toString(); } } 

line data = (response.getEntity (genericType)); cause error

this code worked with an old T-shirt, but now, what should I do to solve this error?

+4
source share
2 answers

You can change response.getEntity (genericType) with response.readEntity (genericType)

+2
source

What are you asking for, I replaced it

 response.getEntity(String.class); 

by this

 response.getEntityStream().toString(); 

But there may be other problems associated with Jersey 2, I could get it to work by replacing these imports

 import jersey.spi.container.servlet.ServletContainer; import com.sun.jersey.api.client.ClientResponse; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.WebResource; 

By these

 import org.glassfish.jersey.servlet.ServletContainer; import org.glassfish.jersey.client.ClientResponse; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; 

and in the code (since I used Jetty) I had to replace this

 servletHolder.setInitParameter("com.sun.jersey.config.property.packages", "resources"); 

by this

 servletHolder.setInitParameter("jersey.config.server.provider.packages", "resources"); 

and this one

 WebResource webResource = client.resource("http://url_u_want_to_connect"); ClientResponse response = webResource.accept("application/json") 

by this

 WebTarget webTarget = client.target("http://url_u_want_to_connect"); ClientResponse response = webTarget.request("application/json") 

Finally, here is a link to the “latest” docs (about Jersey 2.x, in 2013)

https://jersey.java.net/documentation/latest/user-guide.html

+1
source

Source: https://habr.com/ru/post/1493610/


All Articles