Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class cast exception in jersey project?

Tags:

java

rest

jersey

I have a jersey rest project and one newbie question. From server side:

throw new WebApplicationException(Response.status(Status.NOT_FOUND)
        .entity("no access token found").build());

From client side

else if (Status.fromStatusCode(response.getStatus()) == Status.NOT_FOUND || Status.fromStatusCode(response.getStatus()) == Status.GONE)
        {
            final VerifyTokenResponse verifyTokenResponse = new VerifyTokenResponse();
            verifyTokenResponse.setError((String) response.getEntity());
            return verifyTokenResponse;
        }

Problem is

java.lang.ClassCastException: org.glassfish.jersey.client.HttpUrlConnector$1 cannot be cast to java.lang.String

Why I can't get error string on client side ? Is this correct (String) response.getEntity() for that ?

like image 768
gaponov Avatar asked Dec 20 '22 00:12

gaponov


1 Answers

You need to use response.readEntity(String.class) instead of (String) response.getEntity()

like image 58
gaponov Avatar answered Jan 05 '23 15:01

gaponov