我使用Spring Framework 4.1.0和Spring HATEOAS 0.16.0爲該應用程序開發Spring Web應用程序和Spring測試客戶端。通過RestTemplate與HAL + JSON CURIE交換Spring超媒體資源
測試客戶端具有如語句:
ResponseEntity<Resource<Calculation>> response = restTemplate.exchange(
calculationsUri,
HttpMethod.POST,
new HttpEntity<Calculation>(calculation),
new ParameterizedTypeReference<Resource<Calculation>>()
);
...其中Calculation
是傑克遜註釋的POJO(例如,@JsonProperty
)。
如果沒有CURIE,RestTemplate.exchange()
調用成功:response.getBody().getLinks()
返回一個非空的非空實例List<Link>
。
我的web應用程序有非標準的鏈接關係,例如「子計算」。我想使用CURIE。
隨着居里,即RestTemplate.exchange()
調用失敗:響應-反序列化代碼拋出org.springframework.http.converter.HttpMessageNotReadableException
,致com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException
: 「無法讀取JSON:無法識別的字段‘姓名’(類org.springframework.hateoas.Link),而不是被標記爲(一個已知屬性「href」])'
特別是,傑克遜未能將CURIE從_links
地圖中的響應JSON反序列化爲List<Link>
型字段org.springframework.hateoas.ResourceSupport.links
。響應JSON的樣子:
{
"_links" : {
"self" : {
"href" : "..."
},
"myNamespace:sub-calculations" : [ {
"href" : "..."
}, {
"href" : "..."
} ],
"curies" : [ {
"href" : ".../{rel}",
"name" : "myNamespace",
"templated" : true
} ]
}
}
我可以怎樣利用RestTemplate.exchange()
獲得資源,其HAL+JSON ("application/hal+json")表示採用居里?
客戶端是否包含Spring HATEOAS? – zeroflagL 2014-10-31 08:38:51
@zeroflagL:是的,客戶端包含Spring HATEOAS。 – 2014-10-31 16:07:00
我不認爲Springhasho與Spring4兼容。 – 2014-10-31 18:20:03