Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Maven在Java Servlet中找不到类_Java_Maven_Gson - Fatal编程技术网

使用Maven在Java Servlet中找不到类

使用Maven在Java Servlet中找不到类,java,maven,gson,Java,Maven,Gson,我试图使用Maven从Google导入Gson库,但当我运行web应用程序时,收到一个错误,即找不到Gson类。我试图将提供的内容放在Gson库的范围内,但它没有做任何事情 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-ins

我试图使用Maven从Google导入Gson库,但当我运行web应用程序时,收到一个错误,即找不到Gson类。我试图将提供的内容放在Gson库的范围内,但它没有做任何事情

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>groupId</groupId>
    <artifactId>Skeleton</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.6.2</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

4.0.0
groupId
骨架
1.0-快照
javax.servlet
servlet api
2.5
假如
com.google.code.gson
格森
2.6.2
编译
TestServlet.java

package servlets;

import com.google.gson.Gson;
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.util.ArrayList;
import java.util.List;

/**
 * Created by Philippe on 2016-05-10.
 */
@WebServlet(name = "/TestServlet")
public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<String> list = new ArrayList<String>() ;
        list.add("item1");
        list.add("item2");
        Gson gson = new Gson() ;
        String json = gson.toJson(list) ;
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);
    }
}
包servlet;
导入com.google.gson.gson;
导入javax.servlet.ServletException;
导入javax.servlet.annotation.WebServlet;
导入javax.servlet.http.HttpServlet;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
/**
*由Philippe于2016年5月10日创建。
*/
@WebServlet(name=“/TestServlet”)
公共类TestServlet扩展了HttpServlet{
私有静态最终长serialVersionUID=1L;
受保护的void doPost(HttpServletRequest请求、HttpServletResponse响应)引发ServletException、IOException{
}
受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
列表=新的ArrayList();
列表。添加(“第1项”);
列表。添加(“第2项”);
Gson Gson=新的Gson();
字符串json=gson.toJson(列表);
setContentType(“应用程序/json”);
响应。setCharacterEncoding(“UTF-8”);
response.getWriter().write(json);
}
}
日志

java.lang.ClassNotFoundException:com.google.gson.gson位于 org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305) 在 org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1139) 位于servlets.TestServlet.doGet(TestServlet.java:34) http.HttpServlet.service(HttpServlet.java:622)位于 javaservlet.http.HttpServlet.service(HttpServlet.java:729)


有什么我遗漏的吗?

只要删除
compile
就可以了,如中所述


com.google.code.gson
格森
2.6.2

只需删除中记录的
编译


com.google.code.gson
格森
2.6.2

您是否尝试过“mvn干净安装”?注意:“提供”表示“目标运行时已经提供”。您的目标运行时是Tomcat。它不提供现成的Gson,所以你必须自己通过网络应用程序提供。谢谢你的回答,现在我更清楚为什么它没有做任何事情。这可能是一个愚蠢的问题,但如果我把它放在依赖项中,Maven不应该提供Gson吗?我应该怎么做来修复我的问题?您是否尝试过“mvn干净安装”?注意:“提供”表示“目标运行时已经提供了”。您的目标运行时是Tomcat。它不提供现成的Gson,所以你必须自己通过网络应用程序提供。谢谢你的回答,现在我更清楚为什么它没有做任何事情。这可能是一个愚蠢的问题,但如果我把它放在依赖项中,Maven不应该提供Gson吗?我应该怎么做来解决我的问题?
<!-- http://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.6.2</version>
</dependency>