获取Spring Data REST API的子资源将返回404 Not Found

时间:2018-10-19 18:06:10

标签: spring spring-mvc spring-data-rest

我正在使用Spring Data REST,并且在访问我的资源时遇到问题。我完全可以访问资源,例如当我打电话时

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

我得到类似的东西

{
  "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"
    }
  }
}

如果我正确理解了这个概念,那么我现在对链接部分中的项目执行GET请求,以访问相关的子资源。但是当我想点击链接到相关实体时,例如:

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

我总是得到HTTP/1.1 404 Not Found作为响应以及以下日志输出:

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!]

我不明白我在做什么错。每个子资源都有其自己的公共存储库,而我正在使用默认的存储库检测策略。

通过访问子资源 curl -i -X GET http://localhost:18080/myapp/api/picklistPositions 但是也是可能的。

我正在使用Spring Data REST,但没有Spring Boot。我的配置如下:

@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);
    }
}

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

似乎我的问题与Fetching & Updating lazy-loaded many-many fields in Spring Data REST有关。 我的关系是延迟加载的,因此作为子资源访问它们时不会获取它们。