Home ResponseBuilder - Long cannot be cast to Collection?
Reply: 1

ResponseBuilder - Long cannot be cast to Collection?

Felipe Wagner
1#
Felipe Wagner Published in 2017-12-07 18:43:25Z

I have this: Map<String, Object> social = new HashMap<String, Object>(); social.put("socialId", socialId); social.put("savedComment", savedComment);

and I'm trying to do this: builder = Response.status(Response.Status.OK).entity(social);

Why am I receiving HTTP Status 500 - java.lang.ClassCastException: java.lang.Long cannot be cast to java.util.Collection, since Object should work with Long and general Object classes? Can't I send two different types of data, so data contains data.socialId (as a Long) and data.savedComment (as an object)?

*By the way, socialId is a Long object, and savedComment is a SocialComment object.

Pankaj Gadge
2#
Pankaj Gadge Reply to 2017-12-07 18:48:59Z

If you look at the API docs for entity(), it says

Any Java type instance for a response entity, that is supported by the runtime can be passed. It is the callers responsibility to wrap the actual entity with GenericEntity if preservation of its generic type is required.

So your map needs to support GenericEntity if you need to preserve it's type in the response.

Source: https://docs.oracle.com/javaee/7/api/javax/ws/rs/core/Response.ResponseBuilder.html#entity-java.lang.Object-

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.309475 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO