每当我在spring数据休息中使用自定义序列化程序时,它会添加一个"内容"包装返回对象的属性,如:
{
"content":{
object properties...
},
_links: {
}
}
编辑:添加配置类
@Configuration
public class JacksonCustomizations {
@Bean
public Module rateModule() {
return new RateModule();
}
@SuppressWarnings("serial")
static class RateModule extends SimpleModule {
public RateModule() {
setMixInAnnotation(Package.class, RateModule.PackageMixin.class);
setMixInAnnotation(Section.class, RateModule.SectionMixin.class);
setMixInAnnotation(MainPart.class, RateModule.MainPartMixin.class);
setMixInAnnotation(SubPart.class, RateModule.SubPartMixin.class);
addSerializer(MaintenanceTask.class, new MaintenanceTaskSerializer());
addDeserializer(Package.class, new PackageDeserializer());
addDeserializer(Section.class, new SectionDeserializer());
addDeserializer(MainPart.class, new MainPartDeserializer());
addDeserializer(MaintenanceTask.class, new MaintenanceTaskDeserializer());
}
@JsonAutoDetect(fieldVisibility=Visibility.NONE, getterVisibility=Visibility.NONE, isGetterVisibility=Visibility.NONE)
static abstract class PackageMixin {
@JsonProperty("name") public abstract String getName();
@JsonProperty("sections") public abstract List<Section> getSections();
}
@JsonAutoDetect(fieldVisibility=Visibility.NONE, getterVisibility=Visibility.NONE, isGetterVisibility=Visibility.NONE)
static abstract class SectionMixin {
@JsonProperty("id") public abstract Long getId();
@JsonProperty("name") public abstract String getName();
}
@JsonAutoDetect(fieldVisibility=Visibility.NONE, getterVisibility=Visibility.NONE, isGetterVisibility=Visibility.NONE)
static abstract class MainPartMixin {
@JsonProperty("name") public abstract String getName();
@JsonProperty("subparts") public abstract List<SubPart> getSubParts();
}
@JsonAutoDetect(fieldVisibility=Visibility.NONE, getterVisibility=Visibility.NONE, isGetterVisibility=Visibility.NONE)
static abstract class SubPartMixin {
@JsonProperty("id") public abstract Long getId();
@JsonProperty("name") public abstract String getName();
}
static class MaintenanceTaskSerializer extends JsonSerializer<MaintenanceTask> {
@Override
public void serialize(final MaintenanceTask value, final JsonGenerator gen, final SerializerProvider serializers)
throws IOException, JsonProcessingException {
gen.writeStartObject();
gen.writeNumberField("id", value.getId());
gen.writeStringField("maintenanceRequirementId", value.getMaintenanceRequirementId());
gen.writeStringField("type", value.getType().toString());
gen.writeStringField("title", value.getTitle());
gen.writeStringField("description", value.getDescription());
gen.writeStringField("note", value.getNote());
gen.writeStringField("effectivity", value.getEffectivity());
gen.writeNumberField("procedureReference", value.getReferenceTask().getId());
gen.writeNumberField("aircraftModel", value.getAircraftModel().getId());
gen.writeNumberField("packageId", value.getPack().getId());
gen.writeNumberField("sectionId", value.getSection().getId());
gen.writeStringField("taskType", value.getTaskType().toString());
gen.writeEndObject();
}
}
}
}
但是当我使用没有自定义序列化程序的spring数据休息序列化时,不会插入该属性。
如何阻止此属性显示?
答案 0 :(得分:0)
这是一个已知问题。
此处有报道:https://jira.spring.io/browse/DATAREST-504
该问题指向此堆栈溢出问题:Different JSON output when using custom json serializer in Spring Data Rest