spring-boot - Spring Data Rest + Spring Boot - findBy* 没有将参数传递给 MongoDB?

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

我有一个使用 spring-data-rest 项目的 spring boot web 应用程序(带码头),我无法从 GET 请求中获取参数以传递给 mongo 查询。

我确定我做错了什么,但需要一些指导。

TL;DR... 搜索/查询 =>

$ http GET localhost:8080/rules/search/findByName?test2  # <-- NOTE 'test2'
HTTP/1.1 200 OK
Content-Type: application/hal+json; charset=UTF-8
Date: Fri, 20 Mar 2015 13:54:35 GMT
Server: Jetty(9.2.9.v20150224)
Transfer-Encoding: chunked
X-Application-Context: application

{}

Mongo 锯 =>

> db.system.profile.find({op:"query", ns: "test.rule"}, {query: 1}). sort({ts:-1}).pretty()
{ "query" : { "name" : null } }    # <-- Note *NOT* 'test2'

长话短说...

我有一个简单的“规则”类,只有一个 ID 和名称。

public class Rule {
    private String id;
    private String name;
// getters/setters removed for brevity.
}

我的存储库公开了一个 findByName() 方法。

@RestResource
interface RuleRepository extends CrudRepository<Rule, String> {
    List<Rule> findByName(@Param("name") String name)
}

当我发布到 spring boot 应用程序时,它工作正常。我可以完美地看到mongo数据库中的数据。

(使用httpie 应用程序发布...)

$ http POST localhost:8080/rules name="test2"
HTTP/1.1 201 Created
Content-Length: 0
Date: Fri, 20 Mar 2015 13:49:02 GMT
Location: http://localhost:8080/rules/550c254e87867064832263b3
Server: Jetty(9.2.9.v20150224)
X-Application-Context: application

蒙戈...

> db.rule.find({})
{ "_id" : ObjectId("550c254e87867064832263b3"), "_class" : "<package>.Rule", "name" : "test2" }

到目前为止,一切看起来都还不错。

$ http GET localhost:8080/rules
HTTP/1.1 200 OK
Content-Type: application/hal+json; charset=UTF-8
Date: Fri, 20 Mar 2015 13:51:36 GMT
Server: Jetty(9.2.9.v20150224)
Transfer-Encoding: chunked
X-Application-Context: application

{  "_embedded": { "rules": [  ... brevity.  Everything is here that should be ...

而且搜索资源看起来没问题。

$ http GET localhost:8080/rules/search
HTTP/1.1 200 OK
Content-Type: application/hal+json; charset=UTF-8
Date: Fri, 20 Mar 2015 13:51:47 GMT
Server: Jetty(9.2.9.v20150224)
Transfer-Encoding: chunked
X-Application-Context: application

{
    "_links": {
        "findByName": {
            "href": "http://localhost:8080/rules/search/findByName{?name}",
            "templated": true
        }
    }
}

但是当我搜索时,没有返回任何内容,并且 mongo 报告查询已传递 null。

$ http GET localhost:8080/rules/search/findByName?test2
HTTP/1.1 200 OK
Content-Type: application/hal+json; charset=UTF-8
Date: Fri, 20 Mar 2015 13:54:35 GMT
Server: Jetty(9.2.9.v20150224)
Transfer-Encoding: chunked
X-Application-Context: application

{}

蒙古...

> db.system.profile.find({op:"query", ns: "test.rule"}, {query: 1}). sort({ts:-1}).pretty()
{ "query" : { "name" : null } }

最佳答案

您的请求应该是 localhost:8080/rules/search/findByName?name=test2。如 localhost:8080/rules/searchHATEOAS 响应所述:

{
    "_links": {
        "findByName": {
            "href": "http://localhost:8080/rules/search/findByName{?name}",
            "templated": true
        }
    }
}

findByName{?name}findByName?name=ABC

关于spring-boot - Spring Data Rest + Spring Boot - findBy* 没有将参数传递给 MongoDB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29168362/

相关文章:

java - Spring Rest Data 如何更改默认格式从 json 响应中删除 "_"下划线

spring-boot - Yml 配置文件 "Inheritance"与 Spring Boot

spring - 将摘录投影添加到 Spring Data Rest 存储库会导致堆栈溢出错误

spring-boot - Elasticsearch 分析器在通过 Spring Data 创建时工作,但在直接从 Postman/curl 创建时失败

java - 如何使用复杂类型的 javax.persistence.criteria.Predicate?

java - Spring数据分页和排序存储库,具有多个字段和日期

spring-boot - WebClient 的 bodyToMono 关于空体预期行为

java - 带有 JWT token 的 Spring Security 在每个请求上加载 spring UserDetails 对象

带有 Jboss eap 6.4 的 Spring Boot 2.0.1

java - 如何在没有 OutOfMemoryError 的情况下迭代大型 Mongo 集合