问题描述
编辑 14/08/14 13:29
我的下一个结论是,我的@RepositoryRestResource CrudRepository 生成的 hal+json 格式不正确.
教程(http://spring.io/guides/gs/accessing-data-rest/) 将超媒体 Rest JPA 实体的输出显示为:(请注意没有rel"元素,并且links"不是数组)
{_链接":{人们" : {"href" : "http://localhost:8080/people{?page,size,sort}"}}}
但是,参考文档(http://docs.spring.io/spring-data/rest/docs/1.1.x/reference/html/intro-chapter.html) 显示输出应该是:
{链接":[{"rel" : "客户","href" : "http://localhost:8080/customer"}, {"rel" : "个人资料","href" : "http://localhost:8080/profile"}}
有人知道这是为什么吗?
======================================
编辑 14/08/14:我的调试更进了一步.通过提供我自己的 org.springframework.hateoas.ResourceSupport 类实现,它检查 json 中的_links"而不是links",我更进一步.错误是:
无法从 START_OBJECT 令牌中反序列化 java.util.ArrayList 的实例..... 通过引用链:com.ebs.solas.admin.test.SolicitorDTO[\"_links\"]"
这是因为 org.springframework.hateoas.ResourceSupport 类似乎要求 links 属性是一个 json 数组.并且默认情况下,Spring Data 为 Rest Entity 生成的 json+hal 输出不会生成数组(没有方括号):
"_links" : {自己" : {"href" : "http://localhost:9090/solas-admin-data-api/solicitorFirms/Fxxx"},律师":{"href" : "http://localhost:9090/solas-admin-data-api/solicitorFirms/Fxxx/solicitor}}
希望来自 Spring 论坛的人可以在这里帮助我.
================================================/p>
请查看我的 Spring Data 存储库代码大纲:
@RepositoryRestResource公共接口 SolicitorFirmRepository 扩展了 CrudRepository{}@实体@休息资源@Table(name="XXXX", schema = "XXX")公共类 SolicitorFirm 实现了 Serializable {}
这成功生成了以下 hatoas 资源:
{"firmNumber" : "FXXXX",律师类型":r","公司名称": "XXXX","address1" : "XXXX",地址2":XXX",地址3":XXX",地址4":空,"电话号码": "XXXXX","传真号码": "XXXXX","县" : "OY",_链接":{自己" : {"href" : "http://localhost:9090/solas-admin-data-api/solicitorFirms/XXXX"},律师":{"href" : "http://localhost:9090/solas-admin-data-api/solicitorFirms/XXXX/solicitors"}}
但是,当我为客户端/控制器使用定义 DTO 时:
import org.springframework.hateoas.ResourceSupport;公共类 SolicitorFirmDTO 扩展 ResourceSupport {.....}
并使用以下代码
RestTemplate rt = new RestTemplate();String uri = new String("//xxxxx:9090/solas-admin-data-api/solicitors/Sxxxxx");SolicitorFirmDTO u = rt.getForObject(uri, SolicitorFirmDTO.class, "SXXXX");
我收到以下错误:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:无法识别的字段_links"(com.ebs.solas.admin.test.SolicitorFirmDTO 类),未标记为可忽略(7 个已知属性:xx])
出于某种原因,Spring Data Rest 生成的 json 在 _links
下添加了实体链接,而 HATEOAS 资源超类需要 links
?
有人可以帮忙吗?这是版本问题还是我需要一些额外的配置来将 _links
映射到 links
我尝试了 MappingJackson2HttpMessageConverter
和各种媒体类型 application/json+hal
都无济于事.
也与 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
一起使用 @JsonIgnoreProperties(ignoreUnknown = true)
在每个实体上:
@Entity@JsonIgnoreProperties(ignoreUnknown = true)公共类用户{...}
Edit 14/08/14 13:29
My next conclusion is that the hal+json format produced from my @RepositoryRestResource CrudRepository is incorrect.
The tutorial (http://spring.io/guides/gs/accessing-data-rest/) shows the output of a hypermedia Rest JPA entity as: (please note there is no "rel" element, and "links" is not an array)
{
"_links" : {
"people" : {
"href" : "http://localhost:8080/people{?page,size,sort}"
}
}
}
However, the reference docs (http://docs.spring.io/spring-data/rest/docs/1.1.x/reference/html/intro-chapter.html) show that the output should be:
{
"links" : [ {
"rel" : "customer",
"href" : "http://localhost:8080/customer"
}, {
"rel" : "profile",
"href" : "http://localhost:8080/profile"
}
}
Does anyone know why this is?
=====================================
Edit 14/08/14: I have taken my debugging a step further. By providing my own implementation of a org.springframework.hateoas.ResourceSupport class, which inspects the json for "_links" rather than "links" I get a step further. The error is:
This is because the org.springframework.hateoas.ResourceSupport class seems to require that the links attribute be a json array. And by default the json+hal output produced by Spring Data for a Rest Entity does not produce an array (there are no square brackets):
"_links" : {
"self" : {
"href" : "http://localhost:9090/solas-admin-data-api/solicitorFirms/Fxxx"
},
"solicitors" : {
"href" : "http://localhost:9090/solas-admin-data-api/solicitorFirms/Fxxx/solicitor
}
}
Hopefully someone from the Spring forums could help me here.
==============================================
please see an outline of my Spring Data repository code:
@RepositoryRestResource
public interface SolicitorFirmRepository extends CrudRepository<SolicitorFirm, String> {
}
@Entity
@RestResource
@Table(name="XXXX", schema = "XXX")
public class SolicitorFirm implements Serializable {
}
This successfully generates the following hateoas resource:
{
"firmNumber" : "FXXXX",
"solicitorType" : "r",
"companyName" : "XXXX",
"address1" : "XXXX",
"address2" : "XXX",
"address3" : "XXX",
"address4" : null,
"phoneNumber" : "XXXXX",
"faxNumber" : "XXXXX",
"county" : "OY",
"_links" : {
"self" : {
"href" : "http://localhost:9090/solas-admin-data-api/solicitorFirms/XXXX"
},
"solicitors" : {
"href" : "http://localhost:9090/solas-admin-data-api/solicitorFirms/XXXX/solicitors"
}
}
HOWEVER, when i define a DTO for clientside/controller use:
import org.springframework.hateoas.ResourceSupport;
public class SolicitorFirmDTO extends ResourceSupport {
.....
}
and use the following code
RestTemplate rt = new RestTemplate();
String uri = new String("//xxxxx:9090/solas-admin-data-api/solicitors/Sxxxxx");
SolicitorFirmDTO u = rt.getForObject(uri, SolicitorFirmDTO.class, "SXXXX");
I get the following error:
For some reason the json produced by Spring Data Rest adds the entity links under _links
while the HATEOAS resource superclass expects links
?
Can any one help? is this a version issue or do I need some extra configuration to map _links
to links
I have tried MappingJackson2HttpMessageConverter
and various media types application/json+hal
to no avail.
Also with mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
use @JsonIgnoreProperties(ignoreUnknown = true)
on every Entity:
@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class User {
...
}
这篇关于Spring Data Rest - _links的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!