【问题标题】:Spring Data Rest + Spring Boot - findBy* not passing parameter to MongoDB?Spring Data Rest + Spring Boot - findBy* 没有将参数传递给 MongoDB?
【发布时间】:2015-05-23 23:17:34
【问题描述】:

我有一个使用 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

{}

蒙古锯 =>

> 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 将查询报告为传递了一个空值。

$ 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
  • 该死,你是对的。这很好用。谢谢。 (如果您将此作为答案,我很乐意“检查”它是否正确。)
  • 好的,我会这样做的。 :-D

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


【解决方案1】:

您的请求应该是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

【讨论】:

  • 谢谢(再次)。 {?name} 扔给我 - 以为这意味着把你的名字放在这里。
  • 我为此搜索了文档,但没有找到。 :-/
猜你喜欢
  • 1970-01-01
  • 2020-02-09
  • 1970-01-01
  • 2018-10-25
  • 1970-01-01
  • 2014-11-17
  • 2016-10-28
  • 2019-09-13
  • 2014-06-05
相关资源
最近更新 更多