I was in a similar situation to your question: I had read through the question/answers you linked and found none of them could solve the problem. Here was the eventual answer to my problem:
@Component
public class MyResourceProcessor implements ResourceProcessor<Resource<MyInterface>> {
@Autowired
private EntityLinks entityLinks;
@Override
public Resource<MyInterface> process(Resource<MyInterface> event) {
event.add(entityLinks.linkForSingleResource(MyClass.class, event.getContent().getId()).slash("custom").withRel("custom"));
return event;
}
}
This process
method was called for each Resource in the relevant collection of Resources being returned. The MyInterface
interface and MyClass
class should be replaced with whatever you end up needing, but it was required to be written that way to get this to work. Here are the steps I used to get the process
method called correctly and to determine the MyInterface
type.
I created a process
method that simply took ResourceSupport
as the parameter. I created a breakpoint in the code and inspected what underlying class was extending ResourceSupport
. In my case it was PersistentEntityResource
. This explained why using Resource<MyClass>
or Resources<MyClass>
were never causing the process
method to be called: PersistentEntityResource
extends Resource<Object>
.
I updated the process
method take PersistentEntityResource
as the parameter. This caused the process
method to be called for more than my intended changes. I once again used the breakpoint to inspect the PersistentEntityResource
object with the intent of discovering what class could be found in the Resource<Object>
that it extended. I discovered it was a Proxy
class, and could not be cast to MyClass
as I had desired.
I followed the answer found here to find out more information about the Proxy
class: https://mcmap.net/q/176425/-how-do-i-get-the-underlying-type-of-a-proxy-object-in-java. While debugging, I discovered the list of interfaces that helped define this class. One of them was of type MyProjectionInterface
. I now knew that the reason I couldn't use Resource<Portal>
was because it was actually a Resource<MyProjectionInterface>
.
I had three different Projections
that I needed to handle. Instead of creating three separate ResourcePorcoessors
I created the MyInterface
and had all three of my projection
interfaces extend it. The MyInterface
only contained a Long getId()
method which all of the projections
already supported anyway.
I updated my code to use Resource<MyInterface>
and added the linkForSingleResource
using MyClass
(that all of the projections
relate to) and the getId()
method I had defined in MyInterface
. This successfully added the desired link to each of the resources being returned.
Hopefully these steps help others discover how to determine what type to use as the parameter for the process
method.
RepresentationModelProcessor<PagedModel<EntityModel<T>>>
. HTH someone else. – Otherwise