2016-05-02 65 views
0

我正在使用Java編寫的Web地圖服務以及使用GeoTools庫。我的目標是使數據解析JSON格式,所以我使用的是form Unsupported GeoTools。這是parsingJSON方法,它在輸入文件並返回作爲輸出的文件的FeatureCollection功能的路徑:Java Servlet:使用Geotools解析geo JSON

public FeatureCollection parsingJSON(String path) throws FileNotFoundException, IOException, ParseException { 
     System.out.println("INTO PARSINGJSON"); 
     JSONParser parser = new JSONParser(); 

     File f = new File(path); 
     System.out.println("complete path file --->" + f.getAbsolutePath()); 

     Object obj = parser.parse(new java.io.FileReader(path)); 
     FeatureJSON fJSON = new FeatureJSON(); 
     FeatureCollection fc = fJSON.readFeatureCollection(obj.toString()); 
     return fc; 
    } 

當我通過傳遞文件的路徑運行的代碼,使我這個例外:

mag 02, 2016 4:09:42 PM org.apache.catalina.core.StandardWrapperValve invoke 
GRAVE: Servlet.service() for servlet ServletWMS threw exception 
java.lang.ClassNotFoundException: org.json.simple.parser.ParseException 
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680) 
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526) 
    at com.lamiaservlet.servlets.ServletWMS.operation(ServletWMS.java:140) 
    at com.lamiaservlet.servlets.ServletWMS.doGet(ServletWMS.java:51) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:723) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:612) 
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:503) 
    at java.lang.Thread.run(Unknown Source) 

我要澄清的是我導入庫正常json-simple-1.1,我有以下的pom.xml:

<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>Servlet</groupId> 
    <artifactId>Servlet</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 
    <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <geotools.version>13.2</geotools.version> 
    </properties> 
    <build> 
    <sourceDirectory>src</sourceDirectory> 
    <resources> 
     <resource> 
     <directory>src</directory> 
     <excludes> 
      <exclude>**/*.java</exclude> 
     </excludes> 
     </resource> 
    </resources> 
    <plugins> 
     <plugin> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <version>3.1</version> 
     <configuration> 
      <source>1.8</source> 
      <target>1.8</target> 
     </configuration> 
     </plugin> 
     <plugin> 
     <artifactId>maven-war-plugin</artifactId> 
     <version>2.4</version> 
     <configuration> 
      <warSourceDirectory>WebContent</warSourceDirectory> 
      <failOnMissingWebXml>false</failOnMissingWebXml> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 
    <dependencyManagement> 
    <dependencies> 
     <dependency> 
      <groupId>org.geotools</groupId> 
      <artifactId>gt-main</artifactId> 
      <version>${project.version}</version> 
     </dependency> 
     <!-- Third-party dependencies --> 
     <dependency> 
      <groupId>com.googlecode.json-simple</groupId> 
      <artifactId>json-simple</artifactId> 
      <version>1.1</version> 
     </dependency> 

     <dependency> 
      <groupId>org.geotools</groupId> 
      <artifactId>gt-epsg-hsql</artifactId> 
      <version>${project.version}</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>commons-lang</groupId> 
      <artifactId>commons-lang</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.geotools</groupId> 
      <artifactId>gt-geojson</artifactId> 
      <version>${geotools.version}</version> 
     </dependency> 
    </dependencies> 
    </dependencyManagement> 
</project> 

回答

0

其實你並不需要發在您的pom文件中加入json-simple,因爲maven的魔法會自動將其拉入。但它也不會傷害!我猜你的代碼在Eclipse(或其他IDE)中工作正常,但在將其部署到Tomcat時會失敗。

看來,Maven是不捆綁所有依賴到war文件 - 我可以用你的POM中看到的唯一問題是,你使用project.versiongeotools-version,但只定義geotools-version這可能會混淆的Maven。

與您的問題無關,您聲明gt-epsg-hsql作爲測試依賴項,但您將需要它在您的服務中。您也可以更好地使用gt-geojsondatastore模塊,因爲它可以讓您像使用其他GeoTools數據源一樣使用GeoJSON。

編輯

我創建了一個簡單的Maven項目和下面的POM創建工作戰爭文件,當您運行mvn install所有GeoTools罐子包括在內。如果你打算使用引用,那麼你將需要添加一個epsg罐子,如here所述。

<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>com.ianturton.cookbook</groupId> 
    <artifactId>simple</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 

    <name>simple</name> 
    <url>http://maven.apache.org</url> 

    <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <geotools.version>14.3</geotools.version> 
    </properties> 
<repositories> 
    <repository> 
     <id>maven2-repository.dev.java.net</id> 
     <name>Java.net repository</name> 
     <url>http://download.java.net/maven/2</url> 
    </repository> 
    <repository> 
     <id>osgeo</id> 
     <name>Open Source Geospatial Foundation Repository</name> 
     <url>http://download.osgeo.org/webdav/geotools/</url> 
    </repository> 
    <repository> 
     <snapshots> 
     <enabled>true</enabled> 
     </snapshots> 
     <id>imageio-ext-repository</id> 
     <name>imageio-ext Repository</name> 
     <url>http://maven.geo-solutions.it/</url> 
    </repository> 
    <!-- <repository> 
    <snapshots> 
     <enabled>true</enabled> 
    </snapshots> 
    <id>boundless</id> 
    <name>Boundless Maven Repository</name> 
    <url>https://boundless.artifactoryonline.com/boundless/</url> 
    </repository> --> 
    </repositories> 
    <dependencies> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>3.8.1</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.geotools</groupId> 
     <artifactId>gt-geojson</artifactId> 
     <version>${geotools.version}</version> 
    </dependency> 
    </dependencies> 
    <build> 
    <sourceDirectory>src</sourceDirectory> 
    <resources> 
     <resource> 
     <directory>src</directory> 
     <excludes> 
      <exclude>**/*.java</exclude> 
     </excludes> 
     </resource> 
    </resources> 
    <plugins> 
     <plugin> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <version>3.1</version> 
     <configuration> 
      <source>1.8</source> 
      <target>1.8</target> 
     </configuration> 
     </plugin> 
     <plugin> 
     <artifactId>maven-war-plugin</artifactId> 
     <version>2.4</version> 
     <configuration> 
      <warSourceDirectory>WebContent</warSourceDirectory> 
      <failOnMissingWebXml>false</failOnMissingWebXml> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 
</project> 

讓我在war文件中的以下內容:

201006 Wed Apr 20 16:32:56 BST 2016 WEB-INF/lib/gt-api-14.3.jar 
342540 Wed Oct 14 16:41:20 BST 2015 WEB-INF/lib/core-0.26.jar 
96221 Wed Oct 14 16:41:20 BST 2015 WEB-INF/lib/commons-pool-1.5.4.jar 
31693 Wed Oct 14 16:41:22 BST 2015 WEB-INF/lib/GeographicLib-Java-1.44.jar 
16046 Wed Oct 14 17:16:18 BST 2015 WEB-INF/lib/json-simple-1.1.jar 
794991 Wed Oct 14 16:41:28 BST 2015 WEB-INF/lib/jts-1.13.jar 
91347 Wed Oct 14 16:41:20 BST 2015 WEB-INF/lib/jsr-275-1.0-beta-2.jar 
345853 Wed Apr 20 16:33:04 BST 2016 WEB-INF/lib/gt-opengis-14.3.jar 
284220 Wed Oct 14 17:16:20 BST 2015 WEB-INF/lib/commons-lang-2.6.jar 
151304 Wed Oct 14 16:41:28 BST 2015 WEB-INF/lib/jdom-1.1.3.jar 
508935 Wed Apr 20 16:33:02 BST 2016 WEB-INF/lib/gt-metadata-14.3.jar 
11497 Wed Oct 14 16:41:56 BST 2015 WEB-INF/lib/jgridshift-1.0.jar 
1722452 Wed Apr 20 16:32:56 BST 2016 WEB-INF/lib/gt-main-14.3.jar 
63066 Fri May 06 11:52:30 BST 2016 WEB-INF/lib/gt-geojson-14.3.jar 
1171353 Wed Apr 20 16:33:00 BST 2016 WEB-INF/lib/gt-referencing-14.3.jar 
1900631 Wed Oct 14 16:42:16 BST 2015 WEB-INF/lib/jai_core-1.1.3.jar 
+0

我不得不把庫lib目錄中。 [已解決] – maxada

+0

如果您的pom正確,mvn war應該爲您做這件事 –

+0

我無法更正pom.xml文件。不幸的是,我正在開展另一個人發起的項目,這是我可以做的最好的,或者手動添加它們。有人知道告訴我如何調整pom.xml文件嗎? – maxada