0

I'm trying to do an HTTP GET taking a parameter from the URL and then search into 2 file and send back the informations to the user. I've Tried the Java code into a Main Test without any server or request, and it works without any problem and also the taking of parameters from the URL works. There are other classes made because the file is a Json Array of Objects.

Error Message

HTTP Status 500 – Internal Server Error
Type Exception Report

Message org.glassfish.jersey.server.ContainerException: java.io.FileNotFoundException: \AutoCompilersz\src\net\codejava\ws\italy_cap.json (Impossibile trovare il percorso specificato)

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.io.FileNotFoundException: \AutoCompilersz\src\net\codejava\ws\italy_cap.json (Impossibile trovare il percorso specificato)
    org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:408)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:365)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:318)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Java File

package net.codejava.ws;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;

@Path("/form")
public class ComuniResource {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public ArrayList<Cities> comuni(@QueryParam("cap") String cap) throws FileNotFoundException, IOException {
        Foglio1 p=new Foglio1();
        ArrayList <String> istat =  new ArrayList<String>();
        try (JsonReader reader=new JsonReader(new FileReader("/AutoCompilersz/src/net/codejava/ws/italy_cap.json"))) {
            Gson g = new Gson();
            p=g.fromJson(reader, Foglio1.class);
            //  System.out.println(p);
            //System.out.println(p.Foglio1.length);
            for(int i=0;i<p.Foglio1.length;i++) {
                if(p.Foglio1[i].cap.contentEquals(cap)) {
                    //System.out.println(p.Foglio1[i]);
                    istat.add(p.Foglio1[i].istat.toString());
                }
            }
        }
        Foglio_1 pp=new Foglio_1();
        Result result=new Result();
        try(JsonReader readerz=new JsonReader(new FileReader("/AutoCompilersz/src/net/codejava/ws/italy_cities.json"))){
            Gson g=new Gson();
            pp=g.fromJson(readerz, Foglio_1.class);
            //System.out.println(pp);
            //System.out.println(pp.Foglio1.length);
            for(int i=0;i<pp.Foglio1.length;i++) {
                for(int x=0;x<istat.size();x++) {
                    if(pp.Foglio1[i].istat.contentEquals(istat.get(x))) {
                        result.result.add(pp.Foglio1[i]);//pp.Foglio1[i]
                    }
                }
            }
        }
        return result.result;
    }
}

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.codejava.ws</groupId>
    <artifactId>AutoCompilersz</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.29</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.29</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
            <version>2.29</version>
        </dependency>
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.26.0-GA</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>2.29.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-annotations-api</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-catalina-ant</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-catalina-ha</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-ssi</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-storeconfig</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-tribes</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-catalina</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jdt</groupId>
            <artifactId>ecj</artifactId>
            <version>3.21.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-el-api</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jasper-el</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jasper</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jaspic-api</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jsp-api</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-servlet-api</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-api</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-coyote</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-dbcp</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-i18n-cs</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-i18n-de</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-i18n-es</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-i18n-fr</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-i18n-ja</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-i18n-ko</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-i18n-pt-BR</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-i18n-ru</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-i18n-zh-CN</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jdbc</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jni</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-util-scan</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-util</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-websocket</artifactId>
            <version>9.0.37</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-websocket-api</artifactId>
            <version>9.0.37</version>
        </dependency>
    </dependencies>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>13</release>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.3</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>AutoCompilersz</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            [enter image description here][1]<param-name>jersey.config.server.provider.packages</param-name>
            <param-value>net.codejava.ws</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

Hope can you help me with my problem. Thank you :)

The photo of My Project Explorer [1]: https://i.stack.imgur.com/4b2no.png

  • It clearly says it is unable to find the file. I think you are giving a relative path of the file. Have you tried to give it the absolute path? – Morteza Aug 06 '20 at 11:06
  • I'm giving the absolute path inside the project "/AutoCompilersz/src/net/codejava/ws/italy_cap.json", the fact is that when i tested it into a main also with relative path ("italy_cap.json") it worked perfectly – Andrei Dinut Aug 06 '20 at 11:09
  • Use resources to hold files like json. [how-do-i-load-a-file-from-resource-folder](https://stackoverflow.com/questions/15749192/how-do-i-load-a-file-from-resource-folder) – KanekiSenpai Aug 06 '20 at 11:16
  • I had not looked at your file structure image first. Why your resource file is in the `src/net/codeJava/ws`? you should create a new folder inside `src` called `resource` and put your files there. maybe that helps. cheers – Morteza Aug 06 '20 at 11:22
  • if I try to use "InputStream is = classloader.getResourceAsStream("italy_cap")" g.fromJson gives me error because it sees the file as txt – Andrei Dinut Aug 06 '20 at 12:36

1 Answers1

0

I solved it like this:

try (InputStream is = getClass().getResourceAsStream("/resource/italy_cap.json")) {
            InputStreamReader isr = new InputStreamReader(is);
            JsonReader reader=new JsonReader(isr);
            Gson g = new Gson();
            p=g.fromJson(reader, Foglio1.class);