java.lang.NoClassDefFoundError:com / fasterxml / jackson / core / JsonFactory即使添加了所需的导入后也会出现此错误

时间:2014-04-02 18:05:16

标签: java jackson google-custom-search

这是我的代码。我添加了所有依赖项,然后也出现了这样的错误。 谷歌-HTTP-客户jackson2-1.17.0-rc.jar

在此代码中,在 JsonFactory中获得上述错误jsonFactory = new JacksonFactory();

import com.google.api.services.customsearch.Customsearch;
import com.google.api.services.customsearch.model.Search;
import com.google.api.services.customsearch.model.Result;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;

protected SearchResult[] doSearch() {


    HttpRequestInitializer httpRequestInitializer = new HttpRequestInitializer()
    {   
        @Override
        public void initialize(HttpRequest request) throws IOException {
        }
    };


    JsonFactory jsonFactory = new JacksonFactory();
    Customsearch csearch = new Customsearch( new  NetHttpTransport(),  jsonFactory,  httpRequestInitializer);
    Customsearch.Cse.List listReqst;


    try {
            listReqst = csearch.cse().list(query.getQueryString());
            listReqst.setKey(GOOGLE_KEY);
            // set the search engine ID got from API console
            listReqst.setCx("search engine ID"); 
            // set the query string
            listReqst.setQ(query.getQueryString());
            // language chosen is English for search results 
            listReqst.setLr("lang_en"); 
            // set hit position of first search result
            listReqst.setStart((long) firstResult);
            // set max number of search results to return
            listReqst.setNum((long) maxResults);
            //performs search
            Search result = listReqst.execute();
            java.util.List<Result> results =  result.getItems();
            String urls[] = new String [result.size()];
            String snippets[] = new String [result.size()];
            int i=0;
            for (Result r : results){
                urls[i] = r.getLink();
                snippets[i] = r.getSnippet();
                i++;
            }
            return getResults(snippets, urls, true);    
        } catch (IOException e) {
            // TODO Auto-generated catch block
            MsgPrinter.printSearchError(e);
            System.exit(1);
            return null;
        }
    }

请告诉我应该如何修复它。

4 个答案:

答案 0 :(得分:1)

直接回答这个问题(Pavel在评论中回答)。缺少jackson核心lib依赖项: 杰克逊 - 核 - $ x.y.z.jar

答案 1 :(得分:1)

当有2个杰克逊版本时发生在我身上 - codehaus vs.fastlxml。 删除fastxml版本(这是swagger的trans-dependency)解决了这个问题。

<dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-jersey-jaxrs</artifactId>
    <version>1.5.3</version>
    <exclusions>
        <exclusion>
            <groupId>javax.ws.rs</groupId>
            <artifactId>jsr311-api</artifactId>
        </exclusion>
        <exclusion>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
        </exclusion>
        <exclusion>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
        </exclusion>
        <exclusion>
            <groupId>com.fasterxml.jackson.jaxrs</groupId>
            <artifactId>jackson-jaxrs-json-provider</artifactId>
        </exclusion>
        <!--<exclusion>-->
        <!--<groupId>com.fasterxml.jackson.datatype</groupId>-->
        <!--<artifactId>jackson-datatype-joda</artifactId>-->
        <!-- test -->
    </exclusions>
</dependency>

答案 2 :(得分:0)

我遇到了类似的问题,最终发现这是构建路径和依赖项的问题。最简单的(不是最有效的)是将所有google-api-client jar添加到您的项目中它会消失。更好的方法是跟踪并正确添加jacksonFactory

的所有其他依赖项

答案 3 :(得分:0)

下载杰文的Maven from here

然后将其添加到您的依赖项中。