简体   繁体   English

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

[英]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.我正在使用 Spring 数据 REST 并且在访问我的资源时遇到问题。 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.如果我正确理解了这个概念,我现在可以对链接部分中的项目执行 GET 请求以访问相关的子资源。 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:我总是收到HTTP/1.1 404 Not Found作为响应和以下日志 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.但是,也可以通过curl -i -X GET http://localhost:18080/myapp/api/picklistPositions访问子资源。

I'm using Spring Data REST but without Spring Boot.我正在使用 Spring 数据 REST 但没有 Spring 引导。 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 . 看来我的问题与在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.我遇到了类似的问题,但在我的情况下,数据库中的子资源为空或 null,因此在获取子资源时出现 404 错误。 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 Spring 数据 rest 仍会发送指向数据的链接,但开发人员有责任通过访问 url 检查资源是否确实存在或不存在


Hope this helps to catch some hints to the solution希望这有助于捕捉解决方案的一些提示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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