我希望通过Spring Boot with Jackson中的JAXB注释将对象序列化为JSON和XML。
看看这些:
public interface Permission {
boolean isAllowed();
boolean isExpired();
}
@XmlType
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class UserPermissionDto implements Permission {
@XmlElement
protected boolean enabled;
public boolean isEnabled() {
return enabled;
}
public boolean setEnabled(boolean enabled) {
this.enabled = enabled;
}
public boolean isAllowed() {
return enabled;
}
public boolean isExpired() {
return enabled;
}
}
我希望在没有UserPermissionDto
和allowed
属性的情况下序列化expired
。
我已尝试通过ObjectMapper
自定义Jackson2ObjectMapperBuilder
,添加JaxbAnnotationIntrospector
,autoDetectGettersSetters
设置为false
,但同样的问题仍然存在。
@Bean
@Primary
public ObjectMapper objectMapper() {
Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder;
jackson2ObjectMapperBuilder = new Jackson2ObjectMapperBuilder();
return jackson2ObjectMapperBuilder
.annotationIntrospector(new JaxbAnnotationIntrospector(TypeFactory.defaultInstance()))
.autoDetectGettersSetters(false)
.build();
}
我无法弄清楚为什么@XmlAccessorType(XmlAccessType.FIELD)
没有成功。请指教。