spring-data - Spring Boot Rest 搜索 404

标签 spring-data spring-data-jpa spring-boot spring-data-rest

我定期注意到 /search似乎不适用于我的 @RepositoryRestResource :

Spring Boot 输出

2014-08-25 13:37:51.526  INFO 10645 --- [           main] o.s.d.r.w.RepositoryRestHandlerMapping   : Mapped "{[/{repository}/search],methods=[HEAD],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.HttpEntity<?> org.springframework.data.rest.webmvc.RepositorySearchController.headForSearches(org.springframework.data.rest.webmvc.RootResourceInformation)
2014-08-25 13:37:51.526  INFO 10645 --- [           main] o.s.d.r.w.RepositoryRestHandlerMapping   : Mapped "{[/{repository}/search],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.hateoas.ResourceSupport org.springframework.data.rest.webmvc.RepositorySearchController.listSearches(org.springframework.data.rest.webmvc.RootResourceInformation)
2014-08-25 13:37:51.526  INFO 10645 --- [           main] o.s.d.r.w.RepositoryRestHandlerMapping   : Mapped "{[/{repository}/search/{search}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity<java.lang.Object> org.springframework.data.rest.webmvc.RepositorySearchController.executeSearch(org.springframework.data.rest.webmvc.RootResourceInformation,org.springframework.web.context.request.WebRequest,java.lang.String,org.springframework.data.domain.Pageable,org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler)
2014-08-25 13:37:51.526  INFO 10645 --- [           main] o.s.d.r.w.RepositoryRestHandlerMapping   : Mapped "{[/{repository}/search/{search}],methods=[GET],params=[],headers=[],consumes=[],produces=[application/x-spring-data-compact+json],custom=[]}" onto public org.springframework.hateoas.ResourceSupport org.springframework.data.rest.webmvc.RepositorySearchController.executeSearchCompact(org.springframework.data.rest.webmvc.RootResourceInformation,org.springframework.web.context.request.WebRequest,java.lang.String,java.lang.String,org.springframework.data.domain.Pageable,org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler)
2014-08-25 13:37:51.526  INFO 10645 --- [           main] o.s.d.r.w.RepositoryRestHandlerMapping   : Mapped "{[/{repository}/search/{search}],methods=[HEAD],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity<java.lang.Object> org.springframework.data.rest.webmvc.RepositorySearchController.headForSearch(org.springframework.data.rest.webmvc.RootResourceInformation,java.lang.String)

curl /人
curl http://localhost:8080/persons
{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/persons{?page,size,sort}",
      "templated" : true
    }
  },
  "_embedded" : {
    "persons" : [ {
      "firstName" : "Jimmy",
      "lastName" : "Neutron",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/persons/2"
        }
      }
    }, {
      "firstName" : "Jimmy",
      "lastName" : "Page",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/persons/3"
        }
      }
    }, {
      "firstName" : "Jimmy",
      "lastName" : "Johns",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/persons/4"
        }
      }
    } ]
  },
  "page" : {
    "size" : 20,
    "totalElements" : 3,
    "totalPages" : 1,
    "number" : 0
  }

/搜索上的 curl
curl -v http://localhost:8080/persons/search
* Adding handle: conn: 0x7f9903004400
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7f9903004400) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 8080 (#0)
*   Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> GET /persons/search HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost:8080
> Accept: */*
> 
< HTTP/1.1 404 Not Found
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< Content-Length: 0
< Date: Mon, 25 Aug 2014 18:55:27 GMT
< 
* Connection #0 to host localhost left intact

通常我可以重新启动应用程序,它会随机出现 0 个更改:
curl http://localhost:8080/persons
{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/persons{?page,size,sort}",
      "templated" : true
    },
    "search" : {
      "href" : "http://localhost:8080/persons/search"
    }
  },
  "page" : {
    "size" : 20,
    "totalElements" : 0,
    "totalPages" : 0,
    "number" : 0
  }

是否需要 100% 的时间启用搜索功能?

最佳答案

我有同样的问题,并发现导致它。

Spring 仅为您的域类型加载一个存储库以查找搜索资源映射。如果您的域类型有多个存储库,spring 会以某种方式随机选择一个并使用该存储库。如果该方法不包含您的自定义搜索方法,则不会加载。

关于spring-data - Spring Boot Rest 搜索 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25492655/

相关文章:

java - 具有大量实体的规范和 XML 解析 -> 内存不足错误

java - 使用JPA(条件搜索)在springboot中根据除ID之外的其他字段查找表中的所有记录

java - 在 Spring Boot 中编写单元测试,但由于转换器类而出现错误

java - 找不到能够从类型 [java.util.LinkedHashMap<?, ?>] 转换为类型 [java.lang.String] 的转换器 - Spring 配置服务器

mysql - Mysql中的微服务实现(关系型数据库结构)

java - 从另一个角度提问

java - spring-data-jpa 中的 JOIN FETCH 不会获取惰性关联

找不到 Spring 数据 jpa 存储库属性

java - Xtend 的 _field 命名和 SpringData 的存储库命名约定

mongodb - 如何通过带有 ALPS/HATEOAS 元数据的 Spring Data MongoDB 存储库公开新的 REST 方法?