
This new object is then passed as the entity of the returned Response object. The original HelloEntity list is wrapped in a GenericEntity created as an anonymous class. GenericEntity> entity = new GenericEntity(helloEntities) List helloEntities = greetingService.loadSampleEntities()
Glassfish for eclipse code#
To get around this problem, the Jakarta API has the .rs.core.GenericEntity wrapper for wrapping generic types, as shown in the code snippet below. But if you have a complex or very custom case where the generic information is needed at runtime to fetch the exact .rs.ext.MessageBodyWriter, then this could be a very big problem. For a lot of cases this may not be of concern. The problem with the above is that type erasure removes the type from the list such that at runtime the passed list becomes List instead of the specific Java type HelloEntity passed at compile time. Return Response.ok(helloEntities).build() For example the code below shows the creation and return of a Response object that has a list of HelloEntity as the return payload to the client. One such situation is in Jakarta REST when the .rs.core.Response object is used to return a generic collection of a specific type. But there are a few cases where type information is needed at runtime for some kind of decision. A lot of the time this shouldn’t be of much concern. However, due to historical reasons of backward compatibility, type information for generics is erased at runtime.

Java generics is a great feature that allows you to have compile time checks for generics.
