我无法使用从springboot存储库生成的HATEOAS链接使用一个宁静的API。
请考虑以下模型:
public class Order {
private UUID id;
private String orderNumber;
private Location location;
}
public class Location {
private String street;
private String town;
}
仅出于可读性考虑,我省略了任何样板代码,例如getter,setter和构造函数。
启动服务后,我创建了一个空的Order
,可以像这样查询:
>curl http://localhost:8080/orders/1
哪个返回:
{
"orderNumber": "ON 0815",
"_links": {
"self": {
"href": "http://localhost:8080/orders/1"
},
"location": {
"href": "http://localhost:8080/orders/1/location"
}
}
}
现在,如果我致电curl http://localhost:8080/orders/1/location
,它将返回HTTP 404 - Not found
,这很合理,因为我尚未设置。
接下来,我创建Location
并通过HTTP POST
进行设置:
curl -XPOST http://localhost/8080/locations \
-H 'Content-Type: application/json' \
-d '{"street": "Bakerstreet 221B", "town": "London"}'
服务回答:
{
"street": "Bakerstreet 221B",
"town": "London",
"_links": {
"self": {
"href": "http://localhost:8080/locations/1"
}
}
}
现在我们说清楚了,我被困住了。 如何将Location
链接到Order
?我的尝试都没有成功。如有任何建议,我将不胜感激。
到目前为止,我已经尝试过:
1。放置对象
>curl -XPUT http://localhost:8080/orders/1 \
-H 'Content-Type: application/json' \
-d '{"orderNumber": "ON 0815", "_links": {"self": {"href": "http://localhost:8080/orders/1"}, "location": {"href": "http://localhost:8080/orders/1/location"}}}'
2。修补对象
>curl -XPATCH http://localhost:8080/orders/1 \
-H 'Content-Type: application/json' \
-d '{"_links": {"self": {"href": "http://localhost:8080/orders/1"}, "location": {"href": "http://localhost:8080/orders/1/location"}}}'
3。发布到链接
>curl -XPOST http://localhost:8080/orders/1/location \
-H 'Content-Type: application/json' \
-d '{"street": "Bakerstreet 221B", "town": "London"}'
答案 0 :(得分:0)
管理摘要
>curl -XPUT http://localhost:8080/orders/1/location \
-H "Content-Type: text/uri-list" \
-d "http://localhost:8080/location/1"
详细说明
如果我使用-I
标志和禁止的方法调用嵌套位置uri,则会列出允许的方法。
>curl -I -XPOST http://localhost:8080/orders/1/location \
-H 'Content-Type: application/json'
HTTP/1.1 405
Allow: GET,PUT,PATCH,DELETE
Content-Length: 0
Date: Mon, 29 Oct 2018 08:59:19 GMT
这意味着此URL上的PUT
必须有效。将此网址放在空的正文中会产生:
>curl -XPUT http://localhost:8080/orders/1/location \
-H 'Content-Type: application/json' \
-d '{}'
{"cause":null,"message":"Must send only 1 link to update a property reference that isn't a List or a Map."}
搜索此消息后,我发现我的Content-Type
是错误的。我需要使用Content-Type: text/uri-list
并添加要设置为正文的位置的网址。
>curl -XPUT http://localhost:8080/orders/1/location \
-H "Content-Type: text/uri-list" \
-d "http://localhost:8080/location/1"