Search code examples
springspring-mvcspring-data-rest

GET a sub resource of a Spring Data REST API returns 404 Not Found


I'm using Spring Data REST and having problems accessing my resources. I'm able to access a resource at all, e.g. when I'm calling

curl -i -X GET http://localhost:18080/myapp/api/picklists/662070

I get something like:

{
  "id" : "662070",

    ...

  "_links" : {
    "self" : {
      "href" : "http://localhost:18080/myapp/api/picklists/662070"
    },
    "picklist" : {
      "href" : "http://localhost:18080/myapp/api/picklists/662070"
    },
    "currentStatus" : {
      "href" : "http://localhost:18080/myapp/api/picklists/662070/currentStatus"
    },
    "picklistPositions" : {
      "href" : "http://localhost:18080/myapp/api/picklists/662070/picklistPositions"
    }
  }
}

If I understand the concept correctly I con now perform GET requests on the items in the links-section to access the related sub-resource. But when I want to follow the link to a related entity like:

curl -i -X GET http://localhost:18080/myapp/api/picklists/662070/picklistPositions

I always get HTTP/1.1 404 Not Found as response and the following log output:

2018-10-19 20:04:44,280 | WARN [http-nio-18080-exec-13] [org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.AbstractHandlerExceptionResolver.resolveException(136)] Resolved [org.springframework.data.rest.webmvc.ResourceNotFoundException: Resource not found!]

I don't understand what I'm doing wrong. Every sub-resource has its own public repository and I'm using the default repository detection strategy.

Accessing the sub-resources via curl -i -X GET http://localhost:18080/myapp/api/picklistPositions however is possible as well.

I'm using Spring Data REST but without Spring Boot. My configuration looks like:

@Configuration
class CustomRestMvcConfiguration {

    private String REST_BASE_PATH = "/api";

    @Bean
    public RepositoryRestConfigurer repositoryRestConfigurer() {
        return new RepositoryRestConfigurerAdapter() {
            @Override
            public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
                config.setBasePath(REST_BASE_PATH);
            }
        };
    }

    @Bean
    public BasePathAwareLinkBuilder basePathAwareLinkBuilder(ServletContext servletContext) {
        URI basePath = URI.create(REST_BASE_PATH.startsWith("/") ? REST_BASE_PATH : "/".concat(REST_BASE_PATH));
        return new BasePathAwareLinkBuilder(servletContext, basePath);
    }
}

Any suggestions?


Solution

  • It seems my problem is related to Fetching & Updating lazy-loaded many-many fields in Spring Data REST. My relationships are lazy-loaded so they are not fetched when accessing them as sub-resources.