Spring Boot,Spring Data,Hibernate WebSphere 8.5.5.9 NullPointer for JPARepository

时间:2017-03-14 19:18:36

标签: spring-boot spring-data websphere spring-data-jpa

我在将Spring Boot应用程序部署到Websphere 8.5.5.9时遇到了很多问题,但这个让我感到有点难过。

当我通过我的CXF Rest端点调用时,我的Autowired JPARepository Bean获得了NullPointer,但是通过SpringBoot CommandLineRunner()方法可以很好地插入数据。

项目代码可在github找到。

我将项目的类加载器配置为PARENT_LAST以使CXF正常工作,并设置了一个' / cxf'

的ContextRoot

我可以看到应用正确加载,找到数据库&创建表等..

要点击端点我转到:host:port / cxf / services / sayHello / all

我正在从WebSphere中获取H2 JNDI数据源:

@Configuration
@EntityScan("com.was.spring.cxf.dto")
@EnableJpaRepositories(
      entityManagerFactoryRef = "entityManagerFactory", 
      basePackages = { "com.was.spring.cxf.repository" })
@Order(1)
public class DSConfiguration {

    private Logger logger = LoggerFactory.getLogger(DSConfiguration.class);

    @Autowired
    private Environment environment;

    @Bean
    public JndiObjectFactoryBean dataSourceFactoryBean() {

        final JndiObjectFactoryBean jofb = new JndiObjectFactoryBean();
        jofb.setJndiName("jdbc/ccbIntDataSource");

        return jofb;
    }

    @Bean
    @Primary
    public DataSource dataSource() {

        final Boolean initDB = false;
        final DataSource dataSource = (DataSource) dataSourceFactoryBean().getObject();
        //final Boolean initDB = environment.getProperty("init.db", Boolean.class, Boolean.FALSE);
        logger.debug("Is initializing DB from sql scripts enabled? init.db={}", initDB);

        if (initDB) {
            logger.debug("Start initializing DB with scripts:");
            final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
            //populator.addScripts(dbInitScripts);
            populator.setSqlScriptEncoding("UTF-8");
            DatabasePopulatorUtils.execute(populator, dataSource);
            logger.debug("End initializing DB");
        }

        return dataSource;
    }

    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager(
            @Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }
}

我设置了我的EMF:

@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = "com.was.spring.cxf")
@Order(2)
public class JPAConfiguration {

    private Logger logger = LoggerFactory.getLogger(JPAConfiguration.class);

    @Autowired
    private DataSource dataSource;

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();

        try {
            logger.info("Using DataSource URL: " + dataSource.getConnection().getMetaData().getURL());
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        em.setDataSource(dataSource);
        em.setPersistenceProviderClass(HibernatePersistence.class);
        em.setPackagesToScan(new String[] { "com.was.spring.cxf.model", "com.was.spring.cxf.dto"});

        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        em.setJpaProperties(getHibernateProperties());

        return em;
    }

    private Properties getHibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", H2Dialect.class.getName());
        properties.put("hibernate.show_sql", "true");
        properties.put("hibernate.hbm2ddl.auto", "create-drop");
        return properties;
    }
}

我有一个非常简单的界面&实施:

// Interface
    @GET
    @Path("/all")
    @Produces(MediaType.APPLICATION_JSON)
    Response getAllCustomers();

// Impl

    @Override
    public Response getAllCustomers() {
        logger.info("INVOKING.....");

        if (repository == null){
            logger.info("REPO IS NULL....");
        }

        Iterable<Customer> customers = repository.findAll();
        for (Customer customer : customers) {
            logger.info(customer.toString());
        }

        return Response.status(Status.OK).entity(customers).build();
    }

最后我有了Repository本身:

@Repository
public interface CustomerRepository extends JpaRepository<Customer, Long> {

    List<Customer> findByLastName(String lastName);
}

但是当我尝试使用JPARepository时,我得到以下异常:

[3/14/17 19:13:46:395 UTC] 00000063 SystemOut     O 2017-03-14 19:13:46.395  INFO 122 --- [ebContainer : 0] c.was.spring.cxf.impl.HelloServiceImpl   : INVOKING.....
[3/14/17 19:13:46:395 UTC] 00000063 SystemOut     O 2017-03-14 19:13:46.395  INFO 122 --- [ebContainer : 0] c.was.spring.cxf.impl.HelloServiceImpl   : REPO IS NULL....
[3/14/17 19:13:46:403 UTC] 00000063 SystemOut     O 2017-03-14 19:13:46.402  WARN 122 --- [ebContainer : 0] o.a.cxf.phase.PhaseInterceptorChain      : Application {http://impl.cxf.spring.was.com/}HelloServiceImpl has thrown exception, unwinding now

org.apache.cxf.interceptor.Fault: null
    at org.apache.cxf.service.invoker.AbstractInvoker.createFault(AbstractInvoker.java:162) ~[cxf-core-3.1.7.jar:3.1.7]
    at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:128) ~[cxf-core-3.1.7.jar:3.1.7]
    at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:189) ~[cxf-rt-frontend-jaxrs-3.1.7.jar:3.1.7]
    at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:99) ~[cxf-rt-frontend-jaxrs-3.1.7.jar:3.1.7]
    at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:59) ~[cxf-core-3.1.7.jar:3.1.7]
    at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:96) ~[cxf-core-3.1.7.jar:3.1.7]
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308) ~[cxf-core-3.1.7.jar:3.1.7]
    at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121) [cxf-core-3.1.7.jar:3.1.7]
    at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:254) [cxf-rt-transports-http-3.1.7.jar:3.1.7]
    at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:234) [cxf-rt-transports-http-3.1.7.jar:3.1.7]
    at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:208) [cxf-rt-transports-http-3.1.7.jar:3.1.7]
    at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:160) [cxf-rt-transports-http-3.1.7.jar:3.1.7]
    at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:180) [cxf-rt-transports-http-3.1.7.jar:3.1.7]
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:299) [cxf-rt-transports-http-3.1.7.jar:3.1.7]
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:223) [cxf-rt-transports-http-3.1.7.jar:3.1.7]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:575) [javax.j2ee.servlet.jar:na]
    at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:274) [cxf-rt-transports-http-3.1.7.jar:3.1.7]
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1232) [com.ibm.ws.webcontainer.jar:na]
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:781) [com.ibm.ws.webcontainer.jar:na]
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:480) [com.ibm.ws.webcontainer.jar:na]
    at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178) [com.ibm.ws.webcontainer.jar:na]
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.invokeTarget(WebAppFilterChain.java:136) [com.ibm.ws.webcontainer.jar:na]
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:97) [com.ibm.ws.webcontainer.jar:na]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) [spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195) [com.ibm.ws.webcontainer.jar:na]
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91) [com.ibm.ws.webcontainer.jar:na]
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105) [spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195) [com.ibm.ws.webcontainer.jar:na]
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91) [com.ibm.ws.webcontainer.jar:na]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) [spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195) [com.ibm.ws.webcontainer.jar:na]
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91) [com.ibm.ws.webcontainer.jar:na]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) [spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195) [com.ibm.ws.webcontainer.jar:na]
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91) [com.ibm.ws.webcontainer.jar:na]
    at org.springframework.boot.web.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:115) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.web.support.ErrorPageFilter.access$000(ErrorPageFilter.java:59) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.boot.web.support.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:90) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.boot.web.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:108) [spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195) [com.ibm.ws.webcontainer.jar:na]
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91) [com.ibm.ws.webcontainer.jar:na]
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:967) [com.ibm.ws.webcontainer.jar:na]
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1107) [com.ibm.ws.webcontainer.jar:na]
    at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3928) [com.ibm.ws.webcontainer.jar:na]
    at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:304) [com.ibm.ws.webcontainer.jar:na]
    at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:1007) [com.ibm.ws.webcontainer.jar:na]
    at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1817) [com.ibm.ws.webcontainer.jar:na]
    at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:200) [com.ibm.ws.webcontainer.jar:na]
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:463) [com.ibm.ws.runtime.jar:na]
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:530) [com.ibm.ws.runtime.jar:na]
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:316) [com.ibm.ws.runtime.jar:na]
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:287) [com.ibm.ws.runtime.jar:na]
    at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214) [na:CCX.CF [o1537.01]]
    at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113) [na:CCX.CF [o1537.01]]
    at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175) [com.ibm.ws.runtime.jar:na]
    at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217) [com.ibm.ws.runtime.jar:na]
    at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161) [com.ibm.ws.runtime.jar:na]
    at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138) [com.ibm.ws.runtime.jar:na]
    at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204) [com.ibm.ws.runtime.jar:na]
    at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775) [com.ibm.ws.runtime.jar:na]
    at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905) [com.ibm.ws.runtime.jar:na]
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1881) [com.ibm.ws.runtime.jar:na]
Caused by: java.lang.NullPointerException: null
    at com.was.spring.cxf.impl.HelloServiceImpl.getAllCustomers(HelloServiceImpl.java:37) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0-internal]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:95) ~[na:1.8.0-internal]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55) ~[na:1.8.0-internal]
    at java.lang.reflect.Method.invoke(Method.java:508) ~[na:2.6 (07-13-2016)]
    at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:180) ~[cxf-core-3.1.7.jar:3.1.7]
    at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96) ~[cxf-core-3.1.7.jar:3.1.7]
    ... 65 common frames omitted

0 个答案:

没有答案