【发布时间】: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