【问题标题】:GET a sub resource of a Spring Data REST API returns 404 Not Found获取 Spring Data REST API 的子资源返回 404 Not Found
【发布时间】:2018-10-19 18:06:09
【问题描述】:

我正在使用 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);
    }
}

有什么建议吗?

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 什么。你有没有解决这个问题?
    • 我必须支持 Soumitri。这不是一个很好的答案。你做了什么来解决它?
    猜你喜欢
    • 2020-01-28
    • 2016-11-19
    • 2021-09-11
    • 2018-04-07
    • 2018-12-10
    • 2022-12-17
    • 2015-08-30
    • 1970-01-01
    • 2018-06-21
    相关资源
    最近更新 更多