我正在尝试使用 Jackson 以二进制格式序列化我的课程,并在尝试了多种不同的替代方案后苦苦挣扎。我希望有人能帮忙。
我的自定义类看起来像:
package com.common;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.util.FileManager;
import org.apache.jena.util.iterator.ExtendedIterator;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.quick.RDFMain;
import com.quick.exim.Importer;
import com.quick.exim.SQEJSONToRDFImporter;
import com.quick.trans.RDFFormulaToSPARQLTranslator;
/**
* A reasoning request, with the request representation based on RDF standards.
*
*
*/
public class RDFRequest{
private SimpleSPARQLQuery query = null;
// TODO: Would it make more sense for context to be a Model?
// private ContextWrapper context = null;
// private ConfigWrapper config = null;
List<Statement> context = null;
List<Statement> config = null;
public RDFRequest(SimpleSPARQLQuery query,List<Statement> context,List<Statement> config) {
this.query = query;
this.context = context;
this.config = config;
}
public RDFRequest(){
}
public RDFRequest(String queryPath, String driverPath, String vehiclePath) throws IOException {
String absoluteQueryPath = RDFMain.class.getClassLoader().getResource(queryPath).getPath();
Importer<ArrayNode, ?> importer = null;
ArrayNode inputNode = null;
RDFNode internalFrom = null;
ObjectMapper mapper = new ObjectMapper();
InputStream is = new FileInputStream(absoluteQueryPath);
JsonNode node = mapper.readTree(is);
RDFFormulaToSPARQLTranslator translator = new RDFFormulaToSPARQLTranslator();
Model inputModel;
RDFRequest request = null;
// Import the query from json
JsonNode formulaNode = node.get("formula");
if (formulaNode != null) {
importer = new SQEJSONToRDFImporter();
inputNode = (ArrayNode) formulaNode;
}
if (importer != null && inputNode != null) {
internalFrom = (RDFNode) importer.importFrom(inputNode);
inputModel = internalFrom.getModel();
query= translator.translate(inputModel);
}
// Load the context data
Model contextData = FileManager.get().loadModel(driverPath, "N3");
contextData = FileManager.get().readModel(contextData, vehiclePath, "N3");
ExtendedIterator<Statement> statements = contextData.listStatements();
context = statements.toList();
return;
}
public SimpleSPARQLQuery getQuery() {
return query;
}
public void setQuery(SimpleSPARQLQuery query) {
this.query = query;
}
public List<Statement> getContext() {
return context;
}
public void setContext(List<Statement> context) {
this.context = context;
}
public List<Statement> getConfig() {
return config;
}
public void setConfig(List<Statement> config) {
this.config = config;
}
}
拿 1
public void sendMessageUsingJackson(RDFRequest message,String topic){
System.out.println("Inside RDF Request For client "+this.clientID+ " Sending Message");
System.out.println("Topic on which "+this.clientID+" is publishing is "+topic);
this.messageProducer = getMessageProducer(topic);
try {
ObjectMapper mapper = new ObjectMapper();
byte[] jsonBytes = mapper.writeValueAsBytes(message);
BytesMessage m = getSession().createBytesMessage();
m.writeBytes(jsonBytes);
this.messageProducer.send(m);
this.messageProducer.close();
} catch (JMSException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
当我执行此操作时,它会抛出:
com.fasterxml.jackson.databind.JsonMappingException: Conflicting getter definitions for property "uri": org.apache.jena.graph.Node#getURI(0 params) vs org.apache.jena.graph.Node#isURI(0 params) (through reference chain: com.nuance.nrf.common.RDFRequest["query"]->com.nuance.nrf.common.SimpleSPARQLQuery["variables"]->java.util.ArrayList[0])
at com.fasterxml.jackson.databind.SerializerProvider._createAndCacheUntypedSerializer(SerializerProvider.java:838)
at com.fasterxml.jackson.databind.SerializerProvider.findValueSerializer(SerializerProvider.java:387)
at com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap.findAndAddSerializer(PropertySerializerMap.java:38)
at com.fasterxml.jackson.databind.ser.std.AsArraySerializerBase._findAndAddDynamic(AsArraySerializerBase.java:270)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:96)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:21)
at com.fasterxml.jackson.databind.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:186)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:569)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:597)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:142)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:569)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:597)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:142)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:118)
at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:2718)
at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsBytes(ObjectMapper.java:2233)
at com.nuance.research.jms.JMSHook.sendMessageUsingJackson(JMSHook.java:334)
at com.nuance.research.main.RILImpl.exchangeMessage(RILImpl.java:28)
at com.nuance.research.main.RILTestClient.main(RILTestClient.java:35)
Caused by: java.lang.IllegalArgumentException: Conflicting getter definitions for property "uri": org.apache.jena.graph.Node#getURI(0 params) vs org.apache.jena.graph.Node#isURI(0 params)
at com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder.getGetter(POJOPropertyBuilder.java:190)
at com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder.getAccessor(POJOPropertyBuilder.java:283)
at com.fasterxml.jackson.databind.ser.BeanSerializerFactory.removeIgnorableTypes(BeanSerializerFactory.java:678)
at com.fasterxml.jackson.databind.ser.BeanSerializerFactory.findBeanProperties(BeanSerializerFactory.java:557)
at com.fasterxml.jackson.databind.ser.BeanSerializerFactory.constructBeanSerializer(BeanSerializerFactory.java:373)
at com.fasterxml.jackson.databind.ser.BeanSerializerFactory.findBeanSerializer(BeanSerializerFactory.java:268)
at com.fasterxml.jackson.databind.ser.BeanSerializerFactory._createSerializer2(BeanSerializerFactory.java:213)
at com.fasterxml.jackson.databind.ser.BeanSerializerFactory.createSerializer(BeanSerializerFactory.java:152)
at com.fasterxml.jackson.databind.SerializerProvider._createUntypedSerializer(SerializerProvider.java:873)
at com.fasterxml.jackson.databind.SerializerProvider._createAndCacheUntypedSerializer(SerializerProvider.java:833)
... 18 more
我已经尝试过使用其他方式,并且从两天以来一直在努力解决它。我必须使用一个名为 Apache Jena 的外部库,我上面遇到的错误来自与该包中的类相关的方法。感谢我能获得的任何帮助或有关如何解决问题的指示。最终,我想将 RDFRequest 对象转换为字节,并将其作为 ByteMessage 通过 JMS 发送。很高兴提供任何澄清。
谢谢你。