I'm trying to create Gson object inside Java Servlet class.
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.");
}
}
I've tried to use
Gson gsonB = new GsonBuilder().create();
The same problem. After I create the Gson
object, the method returns without any of the prints statements being executed or any exception being logged.
But the code below works fine:
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");
}
}
What am I doing wrong? I'm using google gson library v2.3.1
Gson is a mainstream library.
It seems that your issue is related to your server setup: your compile your servlet with Gson library in classpath (and it also is in classpath when you run your program, perhaps from IDE).
But when you put your war to the server (where Gson is missed)
doPost
teminates when encountersnew Gson()
. So make sure that you have Gson library jar in classpath both when compile time and run time.