【问题标题】:GSON library error with Lotus Notes Java Agent - java.lang.NoClassDefFoundError: com.google.gson.JsonObjectLotus Notes Java 代理的 GSON 库错误 - java.lang.NoClassDefFoundError: com.google.gson.JsonObject
【发布时间】:2019-09-04 06:50:44
【问题描述】:

我正在尝试使用 GSON 库在我的 Lotus Notes 应用程序的 Java 代理中将 Java 对象转换为 JSON。我已将 GSON jar 文件添加到 Project->Java Build Path。但是当我运行代理时,我收到错误 - “线程中的异常“AgentThread:JavaAgent”java.lang.NoClassDefFoundError:com.google.gson.JsonObject”。

基本上我想要实现的是从外部 API 获取一些 JSON,然后将其保存在 Lotus Notes 数据库中。我能够发送 HTTP 请求,但我得到的是一个 Java 对象。我想把它转换成 JSON。

这是类文件,

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.validator.routines.UrlValidator;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class CustRestConsumer {
    /**
     * Method for receiving HTTP JSON GET request against a RESTful URL data source.
     * 
     * @param myUrlStr the URL of the REST endpoint
     * @return JsonObject containing the data from the REST response.
     * @throws IOException
     * @throws MalformedURLException
     * @throws ParseException 
     */
    public static JsonObject GetMyRestData( String myUrlStr ) throws IOException, MalformedURLException {
        JsonObject myRestData = new JsonObject();
        try{

            UrlValidator defaultValidator = new UrlValidator();
            if(defaultValidator.isValid(myUrlStr)){

                URL myUrl = new URL(myUrlStr);
                URLConnection urlCon = myUrl.openConnection();
                urlCon.setConnectTimeout(5000);
                InputStream is = urlCon.getInputStream();
                InputStreamReader isR = new InputStreamReader(is);
                BufferedReader reader = new BufferedReader(isR);
                StringBuffer buffer = new StringBuffer();
                String line = "";
                while( (line = reader.readLine()) != null ){
                    buffer.append(line);
                }
                reader.close();
                JsonParser parser = new JsonParser();
                myRestData = (JsonObject) parser.parse(buffer.toString());

                return myRestData;

            }else{
                myRestData.addProperty("error", "URL failed validation by Apache Commmons URL Validator");
                return myRestData;
            }
        }catch( MalformedURLException e ){
            e.printStackTrace();
            myRestData.addProperty("error", e.toString());
            return myRestData;
        }catch( IOException e ){
            e.printStackTrace();
            myRestData.addProperty("error", e.toString());
            return myRestData;
        }
    }
}

我在这里调用函数,

import com.google.gson.JsonObject;

import lotus.domino.*;




public class JavaAgent extends AgentBase {

    public void NotesMain() {

      try {
          Session session = getSession();
          AgentContext agentContext = session.getAgentContext();

          // (Your code goes here)
          Database db = agentContext.getCurrentDatabase();

          String url = "https://jsonplaceholder.typicode.com/todos/1";
          System.out.println("Reached Here - 1");
          JsonObject myStuff = CustRestConsumer.GetMyRestData(url);
          System.out.println("Reached Here - 2");
          System.out.println(myStuff);

          Document newNotesDoc = db.createDocument();
          newNotesDoc.replaceItemValue("Form", "IBMForm");
//          newNotesDoc.replaceItemValue("WebPageUS", dto.title);
          newNotesDoc.computeWithForm(true, false);
          newNotesDoc.save(true, true);

          db.recycle();

      } catch(Exception e) {
          e.printStackTrace();
       }
   }
}

这是 Java 调试控制台输出,

Reached Here - 1
Exception in thread "AgentThread: JavaAgent" java.lang.NoClassDefFoundError: com.google.gson.JsonObject
    at CustRestConsumer.GetMyRestData(Unknown Source)
    at JavaAgent.NotesMain(Unknown Source)
    at lotus.domino.AgentBase.runNotes(Unknown Source)
    at lotus.domino.NotesThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.google.gson.JsonObject
    at lotus.domino.AgentLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:852)

我是 Java 代理的新手,因此我们将不胜感激。 谢谢。

【问题讨论】:

    标签: java json gson lotus-domino javaagents


    【解决方案1】:

    Project->Java Build Path 用于在 XPages 中使用 JAR 文件。我相信jar文件可以直接添加到代理或者脚本库中,但是分离出来会有内存泄漏的问题,所以不推荐。推荐的方法是将它们添加到服务器的 jvm\lib\ext 中,并且可能还有尝试编译代码的客户端。

    还有其他用于在 Domino 中调度 Java 代码的选项,其中一些在 https://www.intec.co.uk/tag/xots-microservice-scheduler-tutorial/ 中进行了介绍,但还有其他可能的富有想象力的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-07
      • 1970-01-01
      • 1970-01-01
      • 2015-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-26
      相关资源
      最近更新 更多