Spring Data Rest中的关联

4

我有一个名为Veículo的实体和另一个名为Agência的实体,Veículo有许多Agência,我想进行这种关联。然而,当我尝试关联URI时,我会收到此错误信息。
有人可以帮我正确地关联吗?

    daniela.morais@tusk:~$ curl -X POST -H "Content-Type:application/json" -d '{"nome": "Agencia"}' http://localhost:8181/api/agencias
    {
      "nome" : "Agencia",
      "createdBy" : "anonymousUser",
      "lastModifiedBy" : "anonymousUser",
      "createdAt" : "2015-07-17T13:20:10.266+0000",
      "lastModified" : "2015-07-17T13:20:10.266+0000",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8181/api/agencias/55a9010a44ae65cbf03ca4c2"
        }
      }
    }daniela.morais@tusk:~$ curl -X POST -H "Content-Type:application/json" -d '{"nome": "veiculo", "tipo": "tipo"}' http://localhost:8181/api/eiculos
    {
      "nome" : "veiculo",
      "tipo" : "tipo",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8181/api/veiculos/55a9015244ae65cbf03ca4c5"
        },
        "contatos" : {
          "href" : "http://localhost:8181/api/veiculos/55a9015244ae65cbf03ca4c5/contatos"
        },
        "agencias" : {
          "href" : "http://localhost:8181/api/veiculos/55a9015244ae65cbf03ca4c5/agencias"
        }
      }
    }
    daniela.morais@tusk:~$ curl -i -X PUT -H "Content-Type: text/uri-list" -d $'http://localhost:8181/api/agencias/55a9010a44ae65cbf03ca4c2' http://localhost:8181/api/veiculos/55a9015244ae65cbf03ca4c5/agencias
HTTP/1.1 404 Not Found
Server: Apache-Coyote/1.1
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Length: 0
Date: Fri, 17 Jul 2015 13:34:48 GMT
1个回答

2
daniela.morais@tusk:~$ curl -X POST -H "Content-Type:application/json" -d '{"nome": "veiculo", "tipo": "tipo"}' http://localhost:8181/api/eiculos

看看这行。应该将http://localhost:8181/api/eiculos更改为http://localhost:8181/api/veiculos吗?
或者是 http://localhost:8181/api/veiculos/55a9015244ae65cbf03ca4c5/agencias应该更改为http://localhost:8181/api/eiculos/55a9015244ae65cbf03ca4c5/agencias
看起来你的404错误是由一个拼写错误/打字错误导致的,其中eiculos不是veiculos

1
我获取了Agência对象。有一个方法,Spring会自动生成所有路径和路由到正确的存储库。但是当我尝试访问http://localhost:8181/api/veiculos/55a9015244ae65cbf03ca4c5/agencias时,我得到了404错误。 - Daniela Morais
似乎URI还有另一个问题。 - Blake Yarbrough
谢谢,但我不知道我的代码是否有效。当我进行PUT请求时,我得到了204的响应,但是当我尝试在/api/veiculos/{id}上进行GET请求时,我遇到了InvalidDataAccessResourceUsageException异常。 - Daniela Morais
如果在更正打字错误或进行其他更改后出现新的错误,请在问题的编辑中发布新的错误以及任何更改。 - Blake Yarbrough

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接