Jackson和JaxB Annotation非常奇怪的问题。请参阅JSON格式不正确的问题。
public interface All {
}
@XmlJavaTypeAdapter(value=AnyAdapter.class, type=Any.class)
public interface Any {
public boolean isKnown();
}
@XmlJavaTypeAdapter(value=AnyAdapter.class, type=Any.class)
public abstract class Known implements Any {
@Override
@XmlTransient
public boolean isKnown() {
return false;
}
public Known(String type) {
super();
this.type = type;
}
String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public abstract void validate();
}
@XmlRootElement
public class Resource extends Known implements All{
public Resource(Integer resourceId, String resourceName) {
super("Resource");
this.resourceId = resourceId;
this.resourceName = resourceName;
}
public Integer getResourceId() {
return resourceId;
}
public void setResourceId(Integer resourceId) {
this.resourceId = resourceId;
}
public String getResourceName() {
return resourceName;
}
public void setResourceName(String resourceName) {
this.resourceName = resourceName;
}
Integer resourceId;
String resourceName;
@Override
public void validate() {
}
}
@XmlRootElement
public class ResourceList extends Known{
public ResourceList(String name) {
super("ResourceList");
this.name = name;
}
List<All> resourceList;
String name;
@XmlElements({
@XmlElement(name="resource", type=Resource.class),
})
public List<All> getResourceList() {
return resourceList;
}
public void setResourceList(List<All> resourceList) {
this.resourceList = resourceList;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void validate() {
}
}
public class JacksonJaxbAdapterImpl {
public ObjectMapper initializeJackson() {
ObjectMapper mapper = new ObjectMapper();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
mapper.setSerializationInclusion(Include.NON_NULL);
// Need to use JaxB Annotation.
JaxbAnnotationModule module = new JaxbAnnotationModule();
mapper.registerModule(module);
SimpleModule mySimpleModule = new SimpleModule();
mySimpleModule.addSerializer(Long.class, new ToStringSerializer());
mySimpleModule.addSerializer(Double.class, new ToStringSerializer());
mapper.registerModule(mySimpleModule);
return mapper;
}
public void writeObject(Object target, Writer writer, boolean isPretty) throws Exception {
ObjectMapper mapper = initializeJackson();
try {
if(isPretty) {
mapper.writerWithDefaultPrettyPrinter().writeValue(writer,target);
} else {
mapper.writeValue(writer,target);
}
} catch (JsonGenerationException e) {
throw new RuntimeException(e);
} catch (JsonMappingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public class JacksonJaxbAdapterTest {
public static void main(String[] args) throws Exception {
JacksonJaxbAdapterTest test = new JacksonJaxbAdapterTest();
ResourceList report = test.makeObject();
StringWriter jacksonWriter = new StringWriter();
JacksonJaxbAdapterImpl jacksonMainClass = new JacksonJaxbAdapterImpl();
jacksonMainClass.initializeJackson();
jacksonMainClass.writeObject(report, jacksonWriter, true);
System.out.println(jacksonWriter);
}
ResourceList makeObject() {
Resource primary = new Resource(12,"primary");
Resource secondary = new Resource(13, "secondary");
List<All> listofRes = new ArrayList<All>();
listofRes.add(primary);
listofRes.add(secondary);
ResourceList report = new ResourceList("Daily");
report.setResourceList(listofRes);
return report;
}
}
JSON输出带有额外的不需要的“资源”标记。
{
"name" : "Daily",
"resourceList" : [ {
"resource" : {
"resourceId" : 12,
"resourceName" : "primary",
"type" : "Resource"
}
}, {
"resource" : {
"resourceId" : 13,
"resourceName" : "secondary",
"type" : "Resource"
}
} ],
"type" : "ResourceList"
}
删除@XmlElements标记(在ResourceList.java中)并仅使用@XmlElement标记 - JSON在没有“resource”标记的情况下变得完美。 @XmlElement(name =“resource”,type = Resource.class)
{
"name" : "Daily",
"resource" : [ {
"resourceId" : 12,
"resourceName" : "primary",
"type" : "Resource"
}, {
"resourceId" : 13,
"resourceName" : "secondary",
"type" : "Resource"
} ],
"type" : "ResourceList"
}
有任何建议吗?