Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IllegalStateException within method with Response paramether

I wrote a simple class to test response reading entity method (if it works as I expect). But it didn't worked well.

When I launch my class I get following error at 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's 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?

like image 460
VanDavv Avatar asked Apr 29 '16 19:04

VanDavv


People also ask

How do I resolve IllegalStateException in Java?

To avoid the IllegalStateException in Java, it should be ensured that any method in code is not called at an illegal or inappropriate time. Calling the next() method moves the Iterator position to the next element.

Why does Java Lang IllegalStateException occur?

Unchecked exception thrown when an attempt is made to reset a buffer when its mark is not defined. Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or Java application is not in an appropriate state for the requested operation.

What is an illegal state exception?

public class IllegalStateException extends RuntimeException. Signals that a method has been invoked at an illegal or inappropriate time. In other words, the Java environment or Java application is not in an appropriate state for the requested operation.

Which line of code will throw the IllegalStateException error in Webdriver during its execution?

setProperty line. Because of this it will throw illegalStateException.


1 Answers

There are two types of Responses, inbound and outbound, though they still use the same interface. Outbound is when you are sending a response from the server-side

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

Inbound is when you are receiving the response on the client-side.

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

The readEntity() method is disabled on the server-side outbound response because you don't need it. It's only used when you need to de-serialize the response from the response stream. But there is none when it's outbound.

If you want the entity on the outbound response, just use Response#getEntity()

like image 103
Paul Samsotha Avatar answered Sep 19 '22 09:09

Paul Samsotha