Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data Rest Adding Link to Root - RepositoryLinksResource cannot be cast to Resource

I'm trying to add a custom link to my API's root document, following this answer: Custom response for root request int the Spring REST HATEOAS with both RepositoryRestResource-s and regular controllers

This is my custom resource processor:

import org.springframework.data.rest.webmvc.RepositoryLinksResource;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.ResourceProcessor;
import org.springframework.hateoas.mvc.ControllerLinkBuilder;
import org.springframework.stereotype.Component;

@Component
public class CustomRootLinksResourceProcessor implements ResourceProcessor<RepositoryLinksResource> {

    @Override
    public RepositoryLinksResource process(RepositoryLinksResource resource) {
         Link link = ControllerLinkBuilder
                .linkTo(ControllerLinkBuilder
                        .methodOn(CustomerController.class)
                        .register(null))
                .withRel("registerCustomer");

        resource.add(link);

        return resource;
    }
}

However, when I query my API I receive the following exception

LOGBACK:19:01:03.425 [http-nio-8080-exec-2] ERROR o.s.d.r.w.RepositoryRestExceptionHandler - org.springframework.data.rest.webmvc.RepositoryLinksResource cannot be cast to org.springframework.hateoas.Resource
java.lang.ClassCastException: org.springframework.data.rest.webmvc.RepositoryLinksResource cannot be cast to org.springframework.hateoas.Resource
    at org.springframework.hateoas.mvc.ResourceProcessorInvoker$DefaultProcessorWrapper.invokeProcessor(ResourceProcessorInvoker.java:224)
    at org.springframework.hateoas.mvc.ResourceProcessorInvoker.invokeProcessorsFor(ResourceProcessorInvoker.java:141)
    at org.springframework.hateoas.mvc.ResourceProcessorInvoker.invokeProcessorsFor(ResourceProcessorInvoker.java:124)
    at org.springframework.hateoas.mvc.ResourceProcessorHandlerMethodReturnValueHandler.handleReturnValue(ResourceProcessorHandlerMethodReturnValueHandler.java:113)
    at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:81)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)

I'm not sure what I'm doing wrong here, as checking the source code the of ResourceProcessorInvoker it appears there's only a cast to ResourceSupport (which RepositoryLinksResource extends, so it should be fine). Is this a bug in spring data rest or is there something wrong with my CustomRootLinksResourceProcessor?

    @Override
    @SuppressWarnings("unchecked")
    public Object invokeProcessor(Object object) {
        return ((ResourceProcessor<ResourceSupport>) processor).process((ResourceSupport) object);
    }
like image 405
Johannes Rudolph Avatar asked Jan 05 '23 04:01

Johannes Rudolph


1 Answers

As Sam mentioned (and all credit to him) "I think ResourceProcessor implementation must not be a lambda just like in the docs"

I could not find the exact reference in the docs, but here is an example just in case other people struggle with this issue.

This works:

@Bean
public ResourceProcessor<Resource<Invoice>> processor() {
    return new ResourceProcessor<Resource<Invoice>>() {
        @Override
        public Resource<Invoice> process(Resource<Invoice> resource) {
            resource.add(entityLinks.linkToSingleResource(Invoice.class, 1));
            return resource;
        }
    };
}

But this does not:

@Bean
public ResourceProcessor<Resource<Invoice>> processor() {
    return (Resource<Invoice> resource) -> {
        resource.add(entityLinks.linkToSingleResource(Invoice.class, 1));
        return resource;
    };
}
like image 115
rjdkolb Avatar answered Jan 13 '23 09:01

rjdkolb