gpt4 book ai didi

Java EE项目找不到Gson

转载 作者:行者123 更新时间:2023-12-01 15:38:18 24 4
gpt4 key购买 nike

我正在为类构建一个 Java EE 项目,并尝试使用 Google 的 Gson 包将 JSON 发送到 Servlet 内项目的另一部分。当我访问映射到上述 Servlet 的 URL 时,我希望看到原始 JSON,但是我得到了 ClassNotFoundException

import com.google.gson.Gson;
import edu.neumont.writingCenter.json.Event;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@WebServlet("/user/appointments/json")
public class UserAppointmentsJsonServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json");
PrintWriter out = response.getWriter();

List<Event> events = new ArrayList<Event>();
for (int i =0; i < 10; i++) {
Event event = new Event();
event.setId(i);
event.setTitle("Test " + i);
event.setAllDay(false);
event.setStart(new Date());
event.setEnd(new Date());
events.add(event);
}

Gson gson = new Gson();
out.print(gson.toJson(events));
out.flush();

}
}

java.lang.NoClassDefFoundError: com/google/gson/Gson at edu.neumont.writingCenter.servlets.user.appointments.UserAppointmentsJsonServlet.doGet(UserAppointmentsJsonServlet.java:35) at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217) at edu.neumont.writingCenter.filters.SetRootFilter.doFilter(SetRootFilter.java:43) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98) at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162) at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174) at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828) at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725) at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019) at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225) at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) at com.sun.grizzly.ContextTask.run(ContextTask.java:71) at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) at java.lang.Thread.run(Thread.java:662) Caused by: java.lang.ClassNotFoundException: com.google.gson.Gson at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1519) at org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1369) ... 33 more

最佳答案

CNF 异常意味着您的 CLASSPATH 中缺少 JAR。尝试将其放入 WAR 文件的 WEB-INF/lib 中,重新打包并重新部署。

如果您遇到另一个 CNF 异常,请继续添加 JAR,直到它们消失。

关于Java EE项目找不到Gson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8513971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com