春天HATEOAS 定义和注册一个HttpMessageConverter
与包含序列化改造其杰克逊模块ResourceSupport
和Resources
类型HAL JSON表示。 这些模块使用杰克逊混入到串行器结合到类型如下所示:
package org.springframework.hateoas.hal;
// Imports omitted
public abstract class ResourcesMixin<T> extends Resources<T> {
@Override
@XmlElement(name = "embedded")
@JsonProperty("_embedded")
@JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY, using = Jackson2HalModule.HalResourcesSerializer.class)
@JsonDeserialize(using = Jackson2HalModule.HalResourcesDeserializer.class)
public abstract Collection<T> getContent();
}
这导致混入延伸类型Resources<T>
以获得序列化和反序列化使用Jackson2HalModule.HalResourcesSerializer.class
。 然而,实现似乎在其能力嵌入有限资源 。 在试图解决这个限制, 我尝试使用从上面杰克逊混入注释使用Spring HATEOAS串行在我自己的类 :
package mypackage;
// Imports omitted
public class ModelAResource extends Resource<ModelA> {
private Collection<Resource<ModelB>> modelBResources;
public ModelAResource(Model model, Collection<Resource<ModelB>> modelBResources) {
super(model);
this.modelBResources = modelBResources;
}
@JsonProperty("_embedded")
@JsonSerialize(include = JsonSerialize.Inclusion.NON_EMPTY, using = Jackson2HalModule.HalResourcesSerializer.class)
@JsonDeserialize(using = Jackson2HalModule.HalResourcesDeserializer.class)
public Collection<Resource<ModelB>> getModelBResources() {
return modelBResources;
}
}
这工作,但导致了以下WARN
小号越来越登录 :
[2015-01-27 23:36:41.469] boot - 12516 WARN [qtp1175418534-17] ---
MappingJackson2HttpMessageConverter: Failed to evaluate serialization for type
[class mypackage.ModelAResource]:
org.springframework.beans.factory.BeanCreationException: Error creating bean
with name
'org.springframework.hateoas.hal.Jackson2HalModule$HalResourcesSerializer':
Instantiation of bean failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to instantiate
[org.springframework.hateoas.hal.Jackson2HalModule$HalResourcesSerializer]: No
default constructor found; nested exception is
java.lang.NoSuchMethodException:
org.springframework.hateoas.hal.Jackson2HalModule$HalResourcesSerializer<init>()
[2015-01-27 23:36:41.478] boot - 12516 WARN [qtp1175418534-17] ---
MappingJackson2HttpMessageConverter: Failed to evaluate serialization for type
[class mypackage.ModelAResource]:
com.fasterxml.jackson.databind.JsonMappingException: Class
org.springframework.hateoas.hal.Jackson2HalModule$HalResourcesSerializer has no
default (no arg) constructor
尽管有这些警告,系列化产生预期的,期望的结果:
{
"id" : 6,
"_links" : {
"self" : {
"href" : "http://localhost:8080/modelA/6"
},
"links" : {
"href" : "http://localhost:8080/modelA/6/modelBs"
}
},
"_embedded" : {
"modelBs" : [ {
"_links": {
"self": {
"href": "http://localhost:8080/modelA/6/modelBs/1"
}
},
"id" : 1
} ]
}
}
那么,为什么这些警告,为什么它会不顾警告工作? 该模块和串行是公开的 。