IllegalStateException inside a method with a Response parameter

I wrote a simple class to test the response reading method (if it works as I expect). But that did not work.

When I run my class, I get the following error in response.readEntity() :

 Exception in thread "main" java.lang.IllegalStateException: Method not supported on an outbound message. at org.glassfish.jersey.message.internal.OutboundJaxrsResponse.readEntity(OutboundJaxrsResponse.java:150) 

And here is the code I wrote

 public static void main(String[] args) { List<Entity> representations = new ArrayList<>(); representations.add(new Entity("foo", "baz", false)); representations.add(new Entity("foo1", "baz1", true)); representations.add(new Entity("foo2", "baz2", false)); Response build = Response.ok(representations).build(); printEntitesFromResponse(build); } public static void printEntitesFromResponse(Response response) { response .readEntity(new GenericType<List<Entity>>() {}) .stream() .forEach(entity -> System.out.println(entity)); } 

What am I doing wrong?

+8
java jax-rs dropwizard
source share
2 answers

There are two types of Response es, inbound and outbound, although they still use the same interface. Outgoing is when you send a response from the server

 Response response = Response.ok(entity).build(); 

Inbox is when you receive a response on the client side.

 Response response = webTarget.request().get(); 

The readEntity method readEntity disabled by the server-side outgoing response because you do not need it. It is used only when you need to _de_serialize the response from the response stream. But no, when is he leaving.

If you want the entity to be in the outgoing response, just use Response#getEntity()

+13
source share

How the method is implemented in glassfish

  @Override 149 public <T> T readEntity(GenericType<T> entityType) throws ProcessingException { 150 throw new IllegalStateException(LocalizationMessages.NOT_SUPPORTED_ON_OUTBOUND_MESSAGE()); 151 } 
0
source share

All Articles