Problem erhalten Daten aus Excel-Datei über Jersey


vector_ever

Mitglied
hallo,

mein Ziel ist es, eine REST-Diensts (Restful sevice) zu bauen, deshalb wird "Jeresy" (javax.ws.rs) benutzt .
Alles was ich tun möchte, ist Daten von Excel Datei extrahiert und in ArrayList hinzufügen, dann wird diese Daten von ArrayList in Jersy code (Rest) benutzt und als Jason Format als Ausgabe gezeigt.

Bei der Ausführung Jeresy code bekomme ich immer den Folgende Fehler:
HTML:
HTTP Status 500 - java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook
 
type Exception report
 
message java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook
 
description The server encountered an internal error that prevented it from fulfilling this request.
 
exception
 
javax.servlet.ServletException: java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook
    com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:420)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause
 
java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook
    JavaBeans.Reader.read(Reader.java:30)
    JavaBeans.ArrList.arrList(ArrList.java:18)
    com.crunchify.restjersey.RestfullJersy.convertFtoC(RestfullJersy.java:21)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
    com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
    com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
    com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
    com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1480)
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1411)
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1360)
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1350)
    com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:538)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:716)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.41 logs.
 
Apache Tomcat/7.0.41

Die Coden die ich benutze,

Apache Pio um Daten aus Excel-Datei zu lesen zu extrahieren
Code:
package JavaBeans;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.json.simple.JSONObject;
 
 
 
public class Reader {
     
    protected static ArrayList col = new ArrayList();
     
 
 
        public void read(){
         
    try {
          
        FileInputStream file = new FileInputStream(new File("d:\\hi.xls"));
          
        //Get the workbook instance for XLS file 
        HSSFWorkbook workbook = new HSSFWorkbook(file);
      
        //Get first sheet from the workbook
        HSSFSheet sheet = workbook.getSheetAt(0);
          
        //Iterate through each rows from first sheet
        Iterator<Row> rowIterator = sheet.iterator();
        while(rowIterator.hasNext()) {
            Row row = rowIterator.next();
             
            //display from the third row until 5th
            if(row.getRowNum()>2 && (row.getRowNum()<5))
            {
            {
 
            //For each row, iterate through each columns
            Iterator<Cell> cellIterator = row.cellIterator();
            while(cellIterator.hasNext()) {
                 
                //Getting the cell contents
                Cell cell = cellIterator.next();
                  
                switch(cell.getCellType()) {
                    case Cell.CELL_TYPE_BOOLEAN:
                        System.out.print(cell.getBooleanCellValue() + "\t\t");
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        System.out.print(cell.getNumericCellValue() + "\t\t");
                        break;
                    case Cell.CELL_TYPE_STRING:
                        System.out.print(cell.getStringCellValue() + "\t\t");
                        break;
                    case Cell.CELL_TYPE_FORMULA:
                        System.out.println(cell.getCellFormula());
                        break;
                              }
                     }
                        }
            }
            //store the values of the third Column
             Cell cell = row.getCell(2); //if (cell.getColumnIndex() == 2)
                   if(cell != null){
                //add the values of the cell to the Arraylist 
                if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) 
                {
                col.add(cell.getNumericCellValue());
                } 
                else if (cell.getCellType() == Cell.CELL_TYPE_STRING) 
                {
                col.add(cell.getRichStringCellValue().getString());
                } 
                else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) 
                {
                col.add(cell.getBooleanCellValue());
                }
                }
            System.out.println("");
             
        }
         
        file.close();
         
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    }

Code von Jeresy (Restful)

Code:
package com.crunchify.restjersey;
 
 
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import org.json.JSONException;
import org.json.JSONObject;
import JavaBeans.Reader;
  
@Path("/Exceltojersy")
public class RestfullJersy extends Reader{
    @GET
    @Produces("application/json")
    public Response convertFtoC() throws JSONException {
         
        Reader read = new Reader();
        read.read();
         
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("coloum 0", col.get(0)); 
                jsonObject.put("coloum 1", col.get(1)); 
                jsonObject.put("coloum 2", col.get(2)); 
                jsonObject.put("coloum 3", col.get(3));
                jsonObject.put("coloum 4", col.get(4));
                //jsonObject.put("coloum 5", col1.get(5)); 
          
                String result = "@Produces(\"application/json\") Output: \n\nArrayList Output: \n\n" + jsonObject;
                return Response.status(200).entity(result).build();
    }
     
}

PS: Jersy Code funktioniert und bekomme ich die erwartete Ergebnisse falls ich Daten von Excel Datei sonder sofort von einer ArrayList unabhängig con Excel Datei. Z.B:

Code:
package JavaBeans;
 
import java.util.ArrayList;
 
public class ArrList1 {
 
protected static ArrayList col1 = new ArrayList();
 
     
    public  void arrList() {
        col1.add(1);
        col1.add(2);
        col1.add(3);
        col1.add(4);
        col1.add(5);
        col1.add(6);
         
        //print the value of the cells which is stored in the the Arraylist
        System.out.println("");
        for (int i = 0; i < col1.size(); i++){
        Object item = col1.get(i);
        System.out.println("New Coloum " + i + " : " + item);
            }
    }
}

benutze diese Code sofort mit Jeresy code dann alles ok, aber ich brauche die Datei unbedingt von der Excel Datei.

web.xml Datei:
HTML:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
<servlet>
  <servlet-name>Jersey Web Application</servlet-name>
  <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
 
<servlet-mapping>
  <servlet-name>Jersey Web Application</servlet-name>
  <url-pattern>/crunchify/*</url-pattern>
</servlet-mapping>
 
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
 
</web-app>

Hat Jemand Ahnung?
 
Zuletzt bearbeitet:

vector_ever

Mitglied
und wie soll sicher sein ob Klasse "org/apache/poi/hssf/usermodel/HSSFWorkbook" im Classpath ist oder nicht?
wie kann ich das tun?
 

hendl

Erfahrenes Mitglied
Falls du in Eclipse entwickelst: Ist die Library im Buildpath des Projekts eingetragen?
Zusätzlich sollte sich die Jar im Verzeichnis /WebContent/WEB-INF/lib/ des Projekts befinden
Lg hendl
 

vector_ever

Mitglied
ok du hast recht, ich habe es nicht richtig eingerichtet, die Libraries waren nicht im Verzeichnis /WebContent/WEB-INF/lib/ des Projekts
habe ich jetzt die hinzugefügt, aber trotzdem klappt nicht, aber ich bekomme andere Fehler:

HTML:
HTTP Status 500 - Servlet.init() for servlet Jersey Web Application threw exception

type Exception report

message Servlet.init() for servlet Jersey Web Application threw exception

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet.init() for servlet Jersey Web Application threw exception
	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
	org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
	org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
	org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
	org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
	java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	java.lang.Thread.run(Unknown Source)
root cause

com.sun.jersey.spi.inject.Errors$ErrorMessagesException
	com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
	com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
	com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
	com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:765)
	com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:760)
	com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:489)
	com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:319)
	com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:609)
	com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
	com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:374)
	com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:557)
	javax.servlet.GenericServlet.init(GenericServlet.java:160)
	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
	org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
	org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
	org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
	org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
	java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	java.lang.Thread.run(Unknown Source)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.41 logs.

wie kann ich die vermeiden?
 

Forum-Statistiken

Themen
272.356
Beiträge
1.558.615
Mitglieder
187.830
Neuestes Mitglied
hansmeiser