[英]Creating GSON object error
我正在嘗試在Java Servlet類中創建Gson對象。
public class SendNews extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Before creating");
Gson gsonObj = new Gson();
System.out.println("This line and the whole code below will be skipped.");
System.out.println("And this method will be finished without Exceptions.");
}
}
我嘗試使用
Gson gsonB = new GsonBuilder().create();
同樣的問題。 創建Gson
對象后,該方法將返回,而不會執行任何prints語句或記錄任何異常。
但是下面的代碼可以正常工作:
public class Main {
public static void main(String[] args) {
//GcmSender.post(new NewsContent());
System.out.println("Before creating");
Gson gsonB = new Gson();
System.out.println("After creating");
}
}
我究竟做錯了什么? 我正在使用Google gson庫v2.3.1
Gson是主流圖書館。
看來問題出在服務器的設置上:在類路徑中用Gson庫編譯servlet(在運行程序時(也許從IDE中)它也在類路徑中)。
但是,當您向服務器發動戰爭 (錯過Gson的地方)時,遇到new Gson()
時doPost
終止。 因此,請確保在編譯時和運行時都在類路徑中有Gson庫jar 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.