You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@karaf.apache.org by slim <sl...@gmail.com> on 2015/08/18 20:51:28 UTC

inject service using cxf

Hello,
I'm a new Karaf user and I'm facing a problem trying to mix Karaf, camel,
cxf and JPA (hibernate)/postgresql and make them to work together.

I followed this tutorial
http://www.liquid-reality.de/display/liquid/2012/01/13/Apache+Karaf+Tutorial+Part+6+-+Database+Access
to understand how to work with JPA, I migrate it to postgresql and it worked
like a charm. Now, I'm trying to call my services through cxf. I created a
sample based on this tutorial:
http://rerun.me/2013/09/01/building-cxf-rest-service-in-osgi-for-karaf/ (the
tutorial worked for me) that combines karaf, cxf, postgresql and JPA but I'm
failing to make it works. the problem is due to a null pointer exception
during REST service call. It seems like the service is not injected when the
service is executed. I tried to use the @PostConstruct annotation on service
method but nothing changed. It keeps providing an error. Anyone faced the
same issue? please let me know if you need a code sample, I will upload it
on a public repo

PS. I'm using Karaf 3.0.4, cxf 2.7.11, postgresql 9.1, JPA 2.1, hibrenate
4.3.6.FINAL: these are the only versions that worked together for me

Many Thanks
Slim



--
View this message in context: http://karaf.922171.n3.nabble.com/inject-service-using-cxf-tp4041915.html
Sent from the Karaf - User mailing list archive at Nabble.com.

Re: inject service using cxf

Posted by slim <sl...@gmail.com>.
Hi Christian,
Thanks again for your answer. Here is another sample using jaxrs:
https://github.com/leadertun/karaf-cxf-jaxrs-postgresql-example/tree/with-database-webservice

I created two branches, one without database and another with a database.
The sample in branch "no database" works perfectly, but I'm keeping
receiving the null pointer exception when using a database. I'm really
confused!!!!

I'm pasting the error below

Many thanks,

Slim

-----------------------

HTTP ERROR 500
Problem accessing /cxf/hello/say/hello/slim. Reason: 
    Server Error

Caused by:
java.lang.RuntimeException: org.apache.cxf.interceptor.Fault
	at
org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:116)
	at
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:336)
	at
org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
	at
org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:241)
	at
org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:248)
	at
org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:222)
	at
org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:153)
	at
org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:171)
	at
org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:286)
	at
org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:211)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:575)
	at
org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:262)
	at Proxyc6a23a63_4fdd_489a_8bb9_a3df0c68eadb.service(Unknown Source)
	at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:684)
	at
org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:503)
	at
org.ops4j.pax.web.service.jetty.internal.HttpServiceServletHandler.doHandle(HttpServiceServletHandler.java:69)
	at
org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
	at
org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557)
	at
org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
	at
org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086)
	at
org.ops4j.pax.web.service.jetty.internal.HttpServiceContext.doHandle(HttpServiceContext.java:240)
	at
org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:429)
	at
org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
	at
org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020)
	at
org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
	at
org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection.handle(JettyServerHandlerCollection.java:75)
	at
org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
	at org.eclipse.jetty.server.Server.handle(Server.java:370)
	at
org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:494)
	at
org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:971)
	at
org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:1033)
	at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:644)
	at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
	at
org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
	at
org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:696)
	at
org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:53)
	at
org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
	at
org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
	at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.cxf.interceptor.Fault
	at
org.apache.cxf.service.invoker.AbstractInvoker.createFault(AbstractInvoker.java:170)
	at
org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:136)
	at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:204)
	at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:101)
	at
org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58)
	at
org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:94)
	at
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272)
	... 37 more
Caused by: java.lang.NullPointerException
	at
com.hello.rest.HelloRestServiceImpl.handleGet(HelloRestServiceImpl.java:17)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at
org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:188)
	at
org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:104)
	... 42 more
Caused by:
org.apache.cxf.interceptor.Fault
	at
org.apache.cxf.service.invoker.AbstractInvoker.createFault(AbstractInvoker.java:170)
	at
org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:136)
	at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:204)
	at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:101)
	at
org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58)
	at
org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:94)
	at
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272)
	at
org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
	at
org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:241)
	at
org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:248)
	at
org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:222)
	at
org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:153)
	at
org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:171)
	at
org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:286)
	at
org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:211)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:575)
	at
org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:262)
	at Proxyc6a23a63_4fdd_489a_8bb9_a3df0c68eadb.service(Unknown Source)
	at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:684)
	at
org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:503)
	at
org.ops4j.pax.web.service.jetty.internal.HttpServiceServletHandler.doHandle(HttpServiceServletHandler.java:69)
	at
org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
	at
org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557)
	at
org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
	at
org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086)
	at
org.ops4j.pax.web.service.jetty.internal.HttpServiceContext.doHandle(HttpServiceContext.java:240)
	at
org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:429)
	at
org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
	at
org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020)
	at
org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
	at
org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection.handle(JettyServerHandlerCollection.java:75)
	at
org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
	at org.eclipse.jetty.server.Server.handle(Server.java:370)
	at
org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:494)
	at
org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:971)
	at
org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:1033)
	at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:644)
	at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
	at
org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
	at
org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:696)
	at
org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:53)
	at
org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
	at
org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
	at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
	at
com.hello.rest.HelloRestServiceImpl.handleGet(HelloRestServiceImpl.java:17)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at
org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:188)
	at
org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:104)
	... 42 more
Caused by:
java.lang.NullPointerException
	at
com.hello.rest.HelloRestServiceImpl.handleGet(HelloRestServiceImpl.java:17)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at
org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:188)
	at
org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:104)
	at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:204)
	at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:101)
	at
org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58)
	at
org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:94)
	at
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272)
	at
org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
	at
org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:241)
	at
org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:248)
	at
org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:222)
	at
org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:153)
	at
org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:171)
	at
org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:286)
	at
org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:211)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:575)
	at
org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:262)
	at Proxyc6a23a63_4fdd_489a_8bb9_a3df0c68eadb.service(Unknown Source)
	at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:684)
	at
org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:503)
	at
org.ops4j.pax.web.service.jetty.internal.HttpServiceServletHandler.doHandle(HttpServiceServletHandler.java:69)
	at
org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
	at
org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557)
	at
org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
	at
org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086)
	at
org.ops4j.pax.web.service.jetty.internal.HttpServiceContext.doHandle(HttpServiceContext.java:240)
	at
org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:429)
	at
org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
	at
org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020)
	at
org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
	at
org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection.handle(JettyServerHandlerCollection.java:75)
	at
org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
	at org.eclipse.jetty.server.Server.handle(Server.java:370)
	at
org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:494)
	at
org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:971)
	at
org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:1033)
	at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:644)
	at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
	at
org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
	at
org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:696)
	at
org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:53)
	at
org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
	at
org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
	at java.lang.Thread.run(Thread.java:745)

Powered by Jetty:// 



--
View this message in context: http://karaf.922171.n3.nabble.com/inject-service-using-cxf-tp4041915p4041981.html
Sent from the Karaf - User mailing list archive at Nabble.com.

Re: inject service using cxf

Posted by Christian Schneider <ch...@die-schneider.net>.
The problem is that there are two instances of 
com.person.rest.RestServiceImpl. One is creatd by blueprint and 
correctly injected.
The other is created by camel-cxf and is not injected with the 
jsonResponseService.

The problem is this:
<cxf:rsServer id="rsServer" address="/person" 
serviceClass="com.person.rest.RestServiceImpl" 
loggingFeatureEnabled="true" />

The property serviceClass means that camel-cxf should create the 
instance of the class. Honestly I have no idea why camel-cxf has this 
option as it is rarely useful.
In general I am not a fan of camel-cxf. It is often highly confusing and 
I had lots of problems with it.

Instead I would use a plain cxf service like described here 
http://cxf.apache.org/docs/jaxrs-services-configuration.html. You refer 
to the impl as a blueprint bean id that can be injected nicely.

If you need camel then I would rather call into the camel route from the 
cxf service impl for example by injecting a camel ProducerTemplate.

Christian

Am 19.08.2015 um 19:16 schrieb slim:
> Hi Christian,
> Many thanks for your answer and your blog. here is a sample for what I'm
> trying to do:
>
> https://github.com/leadertun/karaf-cxf-postgresql-example
>
> yes, I'm using blueprint. the problem is visible when executing the Rest web
> service.  null pointer exception is fired due to a null reference
>
> Error 500 Server Error. Problem accessing /cxf/person/rest/message. Reason:
> Server Error Caused by:
> Caused by: java.lang.NullPointerException
> 	at
> com.person.service.impl.JsonResponseServiceImpl.getJsonResponse(JsonResponseServiceImpl.java:31)
> at
> com.person.rest.RestServiceImpl.getJsonResponseResult(RestServiceImpl.java:27)
>
> Do I miss something?
>
> Many thanks for your help
> Reagrds,
> Slim
>
>
>
> --
> View this message in context: http://karaf.922171.n3.nabble.com/inject-service-using-cxf-tp4041915p4041946.html
> Sent from the Karaf - User mailing list archive at Nabble.com.


Re: inject service using cxf

Posted by slim <sl...@gmail.com>.
Hi Christian,
Many thanks for your answer and your blog. here is a sample for what I'm
trying to do:

https://github.com/leadertun/karaf-cxf-postgresql-example

yes, I'm using blueprint. the problem is visible when executing the Rest web
service.  null pointer exception is fired due to a null reference

Error 500 Server Error. Problem accessing /cxf/person/rest/message. Reason:
Server Error Caused by:
Caused by: java.lang.NullPointerException
	at
com.person.service.impl.JsonResponseServiceImpl.getJsonResponse(JsonResponseServiceImpl.java:31)
at
com.person.rest.RestServiceImpl.getJsonResponseResult(RestServiceImpl.java:27)

Do I miss something?

Many thanks for your help
Reagrds,
Slim



--
View this message in context: http://karaf.922171.n3.nabble.com/inject-service-using-cxf-tp4041915p4041946.html
Sent from the Karaf - User mailing list archive at Nabble.com.

Re: inject service using cxf

Posted by Christian Schneider <ch...@die-schneider.net>.
Hi Slim,

combining cxf and jpa shoul work fine. It would be great if you could 
provide a sample.

If you use blueprint for wiring then you can not use annoations. Instead 
declare the jaxrs service using blueprint xml and inject the jpa bean 
into it using a property.
If you want the jaxrs service and the jpa access in two bundles you can 
use an OSGi service to connect the two.

Christian


On 18.08.2015 20:51, slim wrote:
> Hello,
> I'm a new Karaf user and I'm facing a problem trying to mix Karaf, camel,
> cxf and JPA (hibernate)/postgresql and make them to work together.
>
> I followed this tutorial
> http://www.liquid-reality.de/display/liquid/2012/01/13/Apache+Karaf+Tutorial+Part+6+-+Database+Access
> to understand how to work with JPA, I migrate it to postgresql and it worked
> like a charm. Now, I'm trying to call my services through cxf. I created a
> sample based on this tutorial:
> http://rerun.me/2013/09/01/building-cxf-rest-service-in-osgi-for-karaf/ (the
> tutorial worked for me) that combines karaf, cxf, postgresql and JPA but I'm
> failing to make it works. the problem is due to a null pointer exception
> during REST service call. It seems like the service is not injected when the
> service is executed. I tried to use the @PostConstruct annotation on service
> method but nothing changed. It keeps providing an error. Anyone faced the
> same issue? please let me know if you need a code sample, I will upload it
> on a public repo
>
> PS. I'm using Karaf 3.0.4, cxf 2.7.11, postgresql 9.1, JPA 2.1, hibrenate
> 4.3.6.FINAL: these are the only versions that worked together for me
>
> Many Thanks
> Slim
>
>
>
> --
> View this message in context: http://karaf.922171.n3.nabble.com/inject-service-using-cxf-tp4041915.html
> Sent from the Karaf - User mailing list archive at Nabble.com.


-- 
Christian Schneider
http://www.liquid-reality.de

Open Source Architect
http://www.talend.com