使用Jersey(RESTful服务)+ Hibernate时出错Servlet.init()

时间:2013-05-20 06:21:02

标签: java-ee

我有以下问题: 我在我的项目中将hibernate和jersey的库复制到web-inf / lib中,当我在tomcat中启动项目并键入" localhost:8080 / de.vogella.jersey.jaxb / ind"然后出现错误:

type Exception report

message Servlet.init() for servlet dummyName threw exception

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

exception

javax.servlet.ServletException: Servlet.init() for servlet dummyName 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:947)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
    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

java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(Z)V
    net.sf.cglib.core.DebuggingClassWriter.<init>(DebuggingClassWriter.java:47)
    net.sf.cglib.core.DefaultGeneratorStrategy.getClassWriter(DefaultGeneratorStrategy.java:30)
    net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:24)
    net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
    net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145)
    net.sf.cglib.core.KeyFactory.create(KeyFactory.java:117)
    net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108)
    net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104)
    net.sf.cglib.proxy.Enhancer.<clinit>(Enhancer.java:69)
    org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.getProxyFactory(CGLIBLazyInitializer.java:111)
    org.hibernate.proxy.pojo.cglib.CGLIBProxyFactory.postInstantiate(CGLIBProxyFactory.java:43)
    org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:162)
    org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:135)
    org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:55)
    org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(EntityEntityModeToTuplizerMapping.java:56)
    org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:269)
    org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:425)
    org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:109)
    org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
    org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:226)
    org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1218)
    Pagination.Pagination.init(Pagination.java:30)
    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:947)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
    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)

这是我的WEB.INF:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"   id="WebApp_ID" version="2.5">     

<display-name>Hello Jersey</display-name>
<servlet>   
 <servlet-name>dummyName</servlet-name>     
 <servlet-class>Pagination.Pagination</servlet-class>   
</servlet>

<servlet-mapping>   
 <servlet-name>dummyName</servlet-name>     
 <url-pattern>/ind</url-pattern>    
</servlet-mapping>      

<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>de.vogella.jersey.jaxb</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>   
</servlet>   

<servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>   
</servlet-mapping>

</web-app>

AND PAGINATION.JAVA:

package Pagination;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Projections;

import de.vogella.jersey.jaxb.model.SegmentDetails;

public class Pagination extends HttpServlet
{

SessionFactory factory;

//init method started
public void init(ServletConfig config)throws ServletException
{
factory = new Configuration().configure().buildSessionFactory();
System.out.println("Factory has been created....");
}
//init method end

//service method start
public void service(ServletRequest req, ServletResponse res)
throws ServletException,IOException
{
int pageIndex = 0;
int totalNumberOfRecords = 0;
int numberOfRecordsPerPage = 4;

String sPageIndex = req.getParameter("pageIndex");

if(sPageIndex ==null)
{
pageIndex = 1;
}else
{
pageIndex = Integer.parseInt(sPageIndex);
}

Session ses = factory.openSession();
int s = (pageIndex*numberOfRecordsPerPage) -numberOfRecordsPerPage;

Criteria crit = ses.createCriteria(SegmentDetails.class);
crit.setFirstResult(s);
crit.setMaxResults(numberOfRecordsPerPage);

List l = crit.list();
Iterator it = l.iterator();

PrintWriter pw = res.getWriter();
pw.println("<table border=1>");
pw.println("<tr>");
pw.println("<th>ID</th><th>SPEED</th><th>RELIABILITY</th>");
pw.println("</tr>");

while(it.hasNext())
{
SegmentDetails p = (SegmentDetails)it.next();
pw.println("<tr>");
pw.println("<td>"+p.getSegmentId()+"</td>");
pw.println("<td>"+p.getSpeed()+"</td>");
pw.println("<td>"+p.getReliability()+"</td>");
pw.println("</tr>");
}

pw.println("<table>");

Criteria crit1 = ses.createCriteria(SegmentDetails.class);
crit1.setProjection(Projections.rowCount());

List l1=crit1.list();

// pw.println(l1.size());
//returns 1, as list() is used to execute the query if true will returns 1

Iterator it1 = l1.iterator();

if(it1.hasNext())
{
Object o=it1.next();
totalNumberOfRecords = Integer.parseInt(o.toString());
}

int noOfPages = totalNumberOfRecords/numberOfRecordsPerPage;
if(totalNumberOfRecords > (noOfPages * numberOfRecordsPerPage))
{
noOfPages = noOfPages + 1;
}

for(int i=1;i<=noOfPages;i++)
{
String myurl = "ind?pageIndex="+i;
pw.println("<a href="+myurl+">"+i+"</a>");
}

ses.close();
pw.close();

}
//service method end

//destroy method start
public void destroy()
{
factory.close();
}
//destroy end
}

请帮帮我!提前谢谢

1 个答案:

答案 0 :(得分:0)

检查asm jar的版本。看起来你正在使用的那个与hibernate不兼容。