我定期注意到 /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/