简体   繁体   中英

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, eg 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?

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.

I faced similar issue but in my case sub resource was empty or null in the database hence I was getting 404 error upon fetching of sub-resource. Spring data rest will still send the links to the data but it's developer's responsibility to check whether resource really present or absent by visiting url


Hope this helps to catch some hints to the solution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM