FlexJson 错误:ClassCastException:java.util.HashMap 不能强制转换为类


这是我

尝试使用 FlexJSON 将 LONGTEXT 对象从 mysql 转换回 ProcessInputs java 对象时遇到的错误。

java.lang.ClassCastException: java.util.HashMap cannot be cast to org.juzzy.server.ProcessInputs
    at org.juzzy.server.FLSDAO.RetrieveFLSFromDatabase(FLSDAO.java:68)
    at org.juzzy.client.CreateFLS.doPost(CreateFLS.java:48)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

这就是我的 ProcessInput 类的样子

public class ProcessInputs {
//inputs contain names of all the inputs and their objects
    Map<String,Input> inputs= new LinkedHashMap<String,Input>();
    Map<String,Output> outputs= new HashMap<String,Output>();
    Map<String,Object> fuzzyTerms=new HashMap<String,Object>();
    Map<String,String> fuzzyTermMapper=new HashMap<String,String>();
    ArrayList<T1_Antecedent> antecedentArrayList;
    String outputxml;
    T1_Rulebase  rulebase = new T1_Rulebase(1);
//getters,setters, constructor and functions
}

我在 FLEXJSON 中的检索代码:

while(rs.next()){
        String jsonInString= rs.getString("fls");
        JSONDeserializer<ProcessInputs> der = new JSONDeserializer<ProcessInputs>();
        ProcessInputs fls = der.deserialize(jsonInString);
    }

序列化是这样发生的:

public boolean InsertFLStoDatabase(ProcessInputs object, String xml) throws IOException {
    Connection connection = DatabaseConnect.getConnection();
    String query = "insert into fls_systems (fls, xml) values (?,?)";
    try { 
        JSONSerializer ser = new JSONSerializer();
        String jsonInString = ser.deepSerialize(object);
        PreparedStatement ps = connection.prepareStatement(query);
        ps.setString(1, jsonInString);

从你的例外来看,这应该有效(我猜...

while(rs.next()){
    String jsonInString= rs.getString("fls");
    JSONDeserializer<ProcessInputs> der = new JSONDeserializer<ProcessInputs>();
    // exception is here, right? der.deserialize(String) is giving you a Map!!
    Map<String,Input> fls = der.deserialize(jsonInString);
}

对 flexjson 没有深入的了解,但我不喜欢

Map<String,Input> fls = (Map<String,Input>) der.deserialize(jsonInString);

由于消息:

java.util.HashMap不能投射到org.juzzy.server.ProcessInputs

并且因为 API:

将给定的 json 格式输入反序列化为 Java 对象。

但如果第一个选项不起作用,则可能是一种可能性。

相关内容

  • 没有找到相关文章