ChrisGeo
ChrisGeo

Reputation: 3907

Spring Data Rest ResourceProcessor for Projection Exception

I have created the following Projection

@Projection(name = "select", types = {Organisation.class})
public interface OrganisationSelectProjection {

    Long getId();

    String getName();

}

Which I want to basically use in a "Select" component so I need the least data possible. So I also wanted to remove all the links with a ResourceProcessor, so I created this:

@Bean
public ResourceProcessor<Resource<OrganisationSelectProjection>> organisationProcessor() {
    return resource -> {
        resource.removeLinks();
        return resource;
    };
}

However, it looks like this breaks the entire API since whatever endpoint I hit I get the following exception message org.springframework.hateoas.PagedResources cannot be cast to org.springframework.hateoas.Resource

Any idea what I have doen wrong?

Upvotes: 3

Views: 1161

Answers (1)

Oleg Kurbatov
Oleg Kurbatov

Reputation: 1386

If you'd like to keep anonymous class in place, using ResourceSupport instead of Resource can resolve the issue.

@Bean
public ResourceProcessor<ResourceSupport> organisationProcessor() {
    return resource -> {
        resource.removeLinks();
        return resource;
    };
}

But in this case the processor will be used on each and every resource regardless of type of the content (you can check that inside the method body though).

Instead of anonymous descendant of ResourceProcessor you can define it as a stand-alone class:

public class OrganizationResourceProcessor implements ResourceProcessor<Resource<OrganisationSelectProjection>> {
    @Override
    public Resource<OrganisationSelectProjection> process(Resource<OrganisationSelectProjection> resource) {
        resource.removeLinks();
        return resource;
    }
}

@Bean
public OrganizationResourceProcessor  organisationProcessor() {
    return new OrganizationResourceProcessor();
}

The issue you expereinced has something to do with type erasure since there is no any type parameters in the anonymous class implementation. Your definition is type-safe but it lacks that type-related data which is used at runtime when determining whether particular ResourceProcessor can process a resource. So spring-data-rest thinks that anonymous organizationProcessor can process PagedResources and feeds it to the processor where ClassCastException happens.

Not everything spring-data-rest puts through ResourceProcessors is a Resource. There can be different descendants of org.springframework.hateoas.ResourceSupport class (like PagedResources in your case) and majority of them are not inherited from Resource.

Upvotes: 2

Related Questions