基本的webapp集成了jersey + Spring + hibernate

时间:2015-12-01 08:56:44

标签: spring apache hibernate tomcat jersey

论坛中的类似问题没有答案,因为作为具有Restful Web服务的菜鸟我不得不提出这个问题。

一个mavenized动态Web应用程序,用于以JASON格式从数据库中获取数据。

  • 数据库:apache derby 1.1
  • Eclipse:Juno
  • JDK:1.8
  • 春天:4.2.3
  • Hibernate:5.0
  • 泽西岛:2.19
  • Apache tomcat v8.0
  • 使用的设计模式:none是一个非常基本的集成

设计

  1. 模型类模仿数据库中的表(Spring bean)& JSon模型。
  2. DaoImpl类 - >使用spring支持类进行休眠时,依赖满足的spring bean。
  3. contextWrapper class - >使用@Provider注册,用于调用spring上下文并从DaoImpl类获取结果
  4. RestService类 - >处理rest资源请求从contextWrapper类获取结果
  5. 在服务器日志中编译和部署良好且没有错误但是在访问资源时从jersey抛出NullpointerException。 根据日志Spring上下文没有问题加载。

    Tomcat控制台日志

    Dec 01, 2015 12:11:54 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
    WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:JerseySpringHibernate' did not find a matching property.
    Dec 01, 2015 12:11:54 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Server version:        Apache Tomcat/8.0.24
    Dec 01, 2015 12:11:54 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Server built:          Jul 1 2015 20:19:55 UTC
    Dec 01, 2015 12:11:54 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Server number:         8.0.24.0
    Dec 01, 2015 12:11:54 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: OS Name:               Windows 7
    Dec 01, 2015 12:11:54 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: OS Version:            6.1
    Dec 01, 2015 12:11:54 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Architecture:          x86
    Dec 01, 2015 12:11:54 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Java Home:             C:\Program Files\Java\jre1.8.0_51
    Dec 01, 2015 12:11:54 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: JVM Version:           1.8.0_51-b16
    Dec 01, 2015 12:11:54 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: JVM Vendor:            Oracle Corporation
    Dec 01, 2015 12:11:54 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: CATALINA_BASE:         C:\integration\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
    Dec 01, 2015 12:11:54 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: CATALINA_HOME:         C:\apache-tomcat-8.0.24-windows-x86\apache-tomcat-8.0.24
    Dec 01, 2015 12:11:54 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Command line argument: -Dcatalina.base=C:\integration\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
    Dec 01, 2015 12:11:54 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Command line argument: -Dcatalina.home=C:\apache-tomcat-8.0.24-windows-x86\apache-tomcat-8.0.24
    Dec 01, 2015 12:11:54 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Command line argument: -Dwtp.deploy=C:\integration\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
    Dec 01, 2015 12:11:54 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Command line argument: -Djava.endorsed.dirs=C:\apache-tomcat-8.0.24-windows-x86\apache-tomcat-8.0.24\endorsed
    Dec 01, 2015 12:11:54 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Command line argument: -Dfile.encoding=Cp1252
    Dec 01, 2015 12:11:54 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
    INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre1.8.0_51\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre1.8.0_51/bin/client;C:/Program Files/Java/jre1.8.0_51/bin;C:/Program Files/Java/jre1.8.0_51/lib/i386;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\db-derby-10.11.1.1-bin\db-derby\bin;C:\eclipse-jee-luna-SR1-win32\eclipse;;.
    Dec 01, 2015 12:11:55 AM org.apache.coyote.AbstractProtocol init
    INFO: Initializing ProtocolHandler ["http-nio-8080"]
    Dec 01, 2015 12:11:55 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
    INFO: Using a shared selector for servlet write/read
    Dec 01, 2015 12:11:55 AM org.apache.coyote.AbstractProtocol init
    INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
    Dec 01, 2015 12:11:55 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
    INFO: Using a shared selector for servlet write/read
    Dec 01, 2015 12:11:55 AM org.apache.catalina.startup.Catalina load
    INFO: Initialization processed in 2222 ms
    Dec 01, 2015 12:11:55 AM org.apache.catalina.core.StandardService startInternal
    INFO: Starting service Catalina
    Dec 01, 2015 12:11:55 AM org.apache.catalina.core.StandardEngine startInternal
    INFO: Starting Servlet Engine: Apache Tomcat/8.0.24
    Dec 01, 2015 12:11:57 AM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
    INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [248] milliseconds.
    Dec 01, 2015 12:12:16 AM org.apache.jasper.servlet.TldScanner scanJars
    INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    Dec 01, 2015 12:12:16 AM org.apache.catalina.core.ApplicationContext log
    INFO: No Spring WebApplicationInitializer types detected on classpath
    Dec 01, 2015 12:12:17 AM org.apache.catalina.core.ApplicationContext log
    INFO: Initializing Spring root WebApplicationContext
    Dec 01, 2015 12:12:17 AM org.springframework.web.context.ContextLoader initWebApplicationContext
    INFO: Root WebApplicationContext: initialization started
    Dec 01, 2015 12:12:17 AM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
    INFO: Refreshing Root WebApplicationContext: startup date [Tue Dec 01 00:12:17 PST 2015]; root of context hierarchy
    Dec 01, 2015 12:12:18 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/spring.xml]
    Dec 01, 2015 12:12:19 AM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
    INFO: Loaded JDBC driver: org.apache.derby.jdbc.ClientDriver
    Dec 01, 2015 12:12:21 AM org.hibernate.Version logVersion
    INFO: HHH000412: Hibernate Core {5.0.4.Final}
    Dec 01, 2015 12:12:21 AM org.hibernate.cfg.Environment <clinit>
    INFO: HHH000206: hibernate.properties not found
    Dec 01, 2015 12:12:21 AM org.hibernate.cfg.Environment buildBytecodeProvider
    INFO: HHH000021: Bytecode provider name : javassist
    Dec 01, 2015 12:12:21 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
    INFO: HCANN000001: Hibernate Commons Annotations {5.0.0.Final}
    Dec 01, 2015 12:12:25 AM org.hibernate.dialect.Dialect <init>
    INFO: HHH000400: Using dialect: org.hibernate.dialect.DerbyTenSevenDialect
    Dec 01, 2015 12:12:32 AM org.springframework.web.context.ContextLoader initWebApplicationContext
    INFO: Root WebApplicationContext: initialization completed in 14945 ms
    Dec 01, 2015 12:12:34 AM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler ["http-nio-8080"]
    Dec 01, 2015 12:12:34 AM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler ["ajp-nio-8009"]
    Dec 01, 2015 12:12:34 AM org.apache.catalina.startup.Catalina start
    INFO: Server startup in 40066 ms
    Dec 01, 2015 12:14:21 AM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet [Jersey Web Application] in context with path [/JerseySpringHibernate] threw exception [java.lang.NullPointerException] with root cause
    java.lang.NullPointerException
        at org.shekar.service.RestRequestService.allRows(RestRequestService.java:23)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
        at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:205)
        at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
        at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
        at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:308)
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
        at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
        at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
        at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
        at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:291)
        at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1140)
        at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:403)
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386)
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:334)
        at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
        at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1527)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1484)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Unknown Source)
    

    的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>JerseySpringHibernate</groupId>
      <artifactId>JerseySpringHibernate</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
    
      <properties>
            <jersey.version>2.19</jersey.version>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
       </properties>
    
       <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.glassfish.jersey</groupId>
                    <artifactId>jersey-bom</artifactId>
                    <version>${jersey.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
    
    
    <dependencies>
    <!-- Jersey 2.19 configuration -->
        <dependency>
                <groupId>org.glassfish.jersey.containers</groupId>
                <artifactId>jersey-container-servlet-core</artifactId>
                <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
                <!-- artifactId>jersey-container-servlet</artifactId -->
            </dependency>
    
            <dependency>
                <groupId>org.glassfish.jersey.media</groupId>
                <artifactId>jersey-media-moxy</artifactId>
            </dependency>
    
    
    <!-- Persistence Api » 1.0.2 -->
    
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
    </dependency>
    
    <!-- jersey dependency APIs for CDI (Contexts and Dependency Injection for Java) -->
    <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <version>2.0-EDR1</version>
    </dependency>
    
    <!--Ejb Api » 3.0 -->
    
    <dependency>
        <groupId>javax.ejb</groupId>
        <artifactId>ejb-api</artifactId>
        <version>3.0</version>
    </dependency>
    
    
    <!-- spring configuration -->
    
    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.3.RELEASE</version>
    </dependency>
    
    <!-- Spring JDBC -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.2.3.RELEASE</version>
    </dependency>
    
    
    <!--JSR250 annotation -->
    
    <dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>jsr250-api</artifactId>
    <version>1.0</version>
    </dependency> 
    
    
    <!-- Spring orm -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.2.3.RELEASE</version>
    </dependency>
    
    <!-- Spring web -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.2.3.RELEASE</version>
    </dependency>
    
    <!--java transaction API JTA configuration -->
    
    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>1.1</version>
    </dependency>
    
    <!-- hibernate configuration -->
    
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.0.4.Final</version>
    </dependency>
    
    
    <!-- Derby dirver config for maven -->
    
    
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derbyclient</artifactId>
        <version>10.11.1.1</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <version>10.11.1.1</version>
    </dependency>
    
    
    </dependencies>
    
    
    
      <build>
        <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>webapp</warSourceDirectory>
              <failOnMissingWebXml>false</failOnMissingWebXml>
            </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_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>JerseySpringHibernate</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
      </welcome-file-list>
    
    
      <servlet>
            <servlet-name>Jersey Web Application</servlet-name>
            <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
            <init-param>
                <param-name>jersey.config.server.provider.packages</param-name>
                <param-value>org.shekar</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>Jersey Web Application</servlet-name>
            <url-pattern>/webapi/*</url-pattern>
        </servlet-mapping>
    
        <!-- loading spring context -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>WEB-INF/spring.xml</param-value>
        </context-param>
    
        <listener>
       <listener-class>
            org.springframework.web.context.ContextLoaderListener
       </listener-class>
    </listener> 
    
    
    
    </web-app>
    

    Spring.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    
        <bean id="circle" class="org.shekar.model.CircleModel" scope="prototype"/>
    
        <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" autowire="constructor"> 
        <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/>
        <property name="url" value="jdbc:derby://localhost:1527/test;create=true"/>
        </bean>
    
    
    
        <bean id="sessn" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <property name="dataSource" ref="datasource"/>
            <property name="packagesToScan" value="org.shekar.model"/>
            <property name="hibernateProperties">
                <props>
                    <prop key="dialect">org.hibernate.dialect.DerbyDialect</prop>
                </props>
            </property>
        </bean>
    
    
        <bean id="DaoImpl" class="org.shekar.dao.DaoImpl">
        <property name="sessn" ref="sessn"/>
        </bean>
    
        </beans>
    

    PS: - 任何设计错误都可以原谅我是菜鸟,这篇文章仅供学习之用

1 个答案:

答案 0 :(得分:0)

使用RESTeasy框架而不是Jersey框架来解决RESTFUL固定的NPE问题显然Jersey在Spring 4和Hibernate 5方面存在问题