GET Spring Data REST API returns 404 Not Found 的子资源
GET a sub resource of a Spring Data REST API returns 404 Not Found
我正在使用 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"
}
}
}
如果我正确理解了这个概念,我现在可以对 links 部分中的项目执行 GET 请求以访问相关的子资源。但是当我想跟随 link 到相关实体时,例如:
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!]
我不明白我做错了什么。每个子资源都有自己的 public 存储库,我使用的是默认存储库检测策略。
通过以下方式访问子资源
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);
}
}
有什么建议吗?
看来我的问题与 Fetching & Updating lazy-loaded many-many fields in 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"
}
}
}
如果我正确理解了这个概念,我现在可以对 links 部分中的项目执行 GET 请求以访问相关的子资源。但是当我想跟随 link 到相关实体时,例如:
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!]
我不明白我做错了什么。每个子资源都有自己的 public 存储库,我使用的是默认存储库检测策略。
通过以下方式访问子资源
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);
}
}
有什么建议吗?
看来我的问题与 Fetching & Updating lazy-loaded many-many fields in Spring Data REST 有关。 我的关系是延迟加载的,因此在将它们作为子资源访问时不会被提取。