【发布时间】:2015-08-27 15:16:03
【问题描述】:
我正在使用 Spring Cloud、Spring Data JPA、Spring Data Rest 和 Spring Boot 开发一个 REST API。服务器实现根据 HAL 规范等正确生成各种数据项。它生成 HAL+JSON 如下:
{
"lastModifiedBy" : "unknown",
"lastModifiedOn" : "2015-06-04T12:19:45.249688",
"id" : 2,
"name" : "Item 2",
"description" : null,
"_links" : {
"self" : {
"href" : "http://localhost:8080/pltm/accounts/2"
},
"customers" : {
"href" : "http://localhost:8080/pltm/accounts/2/customers"
},
"users" : {
"href" : "http://localhost:8080/pltm/accounts/2/users"
},
"groups" : {
"href" : "http://localhost:8080/pltm/accounts/2/groups"
}
}
}
现在我正在尝试使用 FeignClient Spring Cloud 库来实现客户端。我已经定义了我的客户端接口如下。
@FeignClient("serviceId")
@RequestMapping(value = "/api", consumes = MediaType.APPLICATION_JSON_VALUE)
public interface PltmClient
{
// Account Requests
@RequestMapping(method = RequestMethod.GET, value = "/accounts")
PagedResources<Resource<Account>> getAccounts();
@RequestMapping(method = RequestMethod.GET, value = "/accounts/{id}")
Resource<Account> getAccountAsResource(@PathVariable("id") Long id);
@RequestMapping(method = RequestMethod.GET, value = "/accounts/{id}")
Account getAccount(@PathVariable("id") Long id);
}
当我调用 getAccout() 方法时,我会从 JSON 文档中获取我的域对象 Account 的详细信息。该对象是一个简单的 POJO。所有字段均已正确填写。
public class Account
{
private Long id;
private String name;
private String description;
/** Setters/Getters left out for brevity **/
}
但是当我调用getAccountAsResource() 时,它可以工作,我会返回一个包含数据的Resource 对象。但是,对Resource.getContent() 的调用会返回一个未完全填充的Account 对象。在这种情况下,Account.getId() 为 NULL,这会导致问题。
任何想法为什么会发生这种情况?我的一个想法是Resource 类定义了getId() 方法,这在某种程度上使Jackson ObjectMapper 感到困惑。
更大的问题是整个方法是否可行,或者是否有更好的方法?显然,我可以只使用普通 POJO 作为我的返回类型,但这会丢失客户端的 HAL 信息。
是否有人为 Spring Data REST 服务器端点成功实现了基于 Java 的客户端实现?
【问题讨论】:
标签: spring-data-rest spring-cloud spring-hateoas