3

Working on a CXF rest service.

This is the interface

 import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    import javax.ws.rs.Produces;

    @Path("/example")
    @Produces("application/json")
    public interface TrackerService {

        @GET
        @Path("/{id}")
        public String get(@PathParam("id") String id);

    }

Implementation class

import org.springframework.stereotype.Service;

import com.service.TrackerService;

@Service("trackerService")
public class TrackerImpl implements TrackerService {

    public String get(String id) {
        // TODO Auto-generated method stub
        return "HELLO";
    }

web.xml, where CXF servlet is defined

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Admin</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/cxf-service.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>



      <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>
            org.apache.cxf.transport.servlet.CXFServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>


</web-app>

cxf-service.xml, where beans are registered

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
       xmlns:cxf="http://cxf.apache.org/core"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
                        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                        http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
                        http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">

    <context:component-scan base-package="com" />

    <jaxrs:server id="trackerCxfServer" address="/">
        <jaxrs:serviceBeans>
            <ref bean="trackerService"/>
        </jaxrs:serviceBeans>
        <jaxrs:extensionMappings>
            <entry key="json" value="application/json"/>
        </jaxrs:extensionMappings>
        <jaxrs:features>
            <cxf:logging/>
        </jaxrs:features>
        <jaxrs:providers>
            <bean class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider" />
        </jaxrs:providers>
    </jaxrs:server>

</beans>

Added it to jboss-6.2 eap , after running getting the below exception

 12:39:58,532 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-2) MSC000001: Failed to start service jboss.deployment.unit."service.war".POST_MODULE: org.jboss.msc.service.StartException in service jboss.deployment.unit."service.war".POST_MODULE: JBAS018733: Failed to process phase POST_MODULE of deployment "service.war"
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:127) [jboss-as-server-7.3.0.Final-redhat-14.jar:7.3.0.Final-redhat-14]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811) [jboss-msc-1.0.4.GA-redhat-1.jar:1.0.4.GA-redhat-1]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746) [jboss-msc-1.0.4.GA-redhat-1.jar:1.0.4.GA-redhat-1]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_65]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_65]
    at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_65]
Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: java.lang.ClassNotFoundException: org.apache.cxf.transport.servlet.CXFServlet from [Module "deployment.service.war:main" from Service Module Loader]
    at org.jboss.as.jaxrs.deployment.JaxrsScanningProcessor.checkDeclaredApplicationClassAsServlet(JaxrsScanningProcessor.java:287)
    at org.jboss.as.jaxrs.deployment.JaxrsScanningProcessor.scanWebDeployment(JaxrsScanningProcessor.java:152)
    at org.jboss.as.jaxrs.deployment.JaxrsScanningProcessor.deploy(JaxrsScanningProcessor.java:103)
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:120) [jboss-as-server-7.3.0.Final-redhat-14.jar:7.3.0.Final-redhat-14]
    ... 5 more
Caused by: java.lang.ClassNotFoundException: org.apache.cxf.transport.servlet.CXFServlet from [Module "deployment.service.war:main" from Service Module Loader]
    at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:197) [jboss-modules.jar:1.3.0.Final-redhat-2]
    at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:443) [jboss-modules.jar:1.3.0.Final-redhat-2]
    at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:431) [jboss-modules.jar:1.3.0.Final-redhat-2]
    at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:373) [jboss-modules.jar:1.3.0.Final-redhat-2]
    at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:118) [jboss-modules.jar:1.3.0.Final-redhat-2]
    at org.jboss.as.jaxrs.deployment.JaxrsScanningProcessor.checkDeclaredApplicationClassAsServlet(JaxrsScanningProcessor.java:285)
Rosh
  • 1,676
  • 4
  • 21
  • 35
  • 2
    Have you in your classpath this libary? "cxf-rt-transports-http-x.x.x.jar" – Xstian Sep 24 '14 at 07:37
  • @Xstian : cxf-rt-transports-http-3.0.0.jar, added error got solved. but no response on hitting the service. http://localhost:8080/service/services/example/1 - Http - 404 error – Rosh Sep 24 '14 at 07:42

1 Answers1

2

You should add this library "cxf-rt-transports-http-x.x.x.jar"

and i see that in your spring configuration missing this import

<import resource="classpath:META-INF/cxf/cxf.xml" /> 
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> 
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

E.G from http://cxf.apache.org/docs/servlet-transport.html

<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:jaxws="http://cxf.apache.org/jaxws"
      xmlns:jaxrs="http://cxf.apache.org/jaxrs"
      xsi:schemaLocation="
         http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
         http://cxf.apache.org/jaxws
         http://cxf.apache.org/schemas/jaxws.xsd
         http://cxf.apache.org/jaxrs
         http://cxf.apache.org/schemas/jaxrs.xsd">

  <import resource="classpath:META-INF/cxf/cxf.xml"/>
  <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
  <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>
  <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

  <jaxws:endpoint id="greeter"
      implementor="org.apache.hello_soap_http.GreeterImpl"
      address="/Greeter1"/>

  <jaxrs:server id="greeterRest"
      serviceClass="org.apache.hello_soap_http.GreeterImpl"
      address="/GreeterRest"/> 

</beans>

By maven dependencies .. I use these dependencies.

     <dependencies>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-rs-extension-providers</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>2.7.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-bindings-soap</artifactId>
            <version>2.7.3</version>
        </dependency>
    </dependencies>
Xstian
  • 8,184
  • 10
  • 42
  • 72
  • @Rosh At the url "/services/*" is shown the list of webservice exposed? Where you declare TrackerService ? – Xstian Sep 24 '14 at 07:53
  • import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; @Path("/example") @Produces("application/json") public interface TrackerService { @GET @Path("/{id}") public String get(@PathParam("id") String id); } – Rosh Sep 24 '14 at 07:57
  • Sorry, i mean the spring bean "TrackerService" that you ref in the jaxws:endpoint .. – Xstian Sep 24 '14 at 07:58
  • @Service("trackerService") public class TrackerImpl implements TrackerService { - it will not work ? Im new to this. – Rosh Sep 24 '14 at 08:02
  • Try to declare as – Xstian Sep 24 '14 at 08:04
  • no luck, any beginning tutorial for Apache cxf rest ? I had refered this one http://idodevjobs.wordpress.com/2014/08/30/develop-a-simple-restful-webservices-using-apache-cxf-and-spring-framework/comment-page-1/#comment-187 – Rosh Sep 24 '14 at 08:10
  • See CXF official [link](http://cxf.apache.org/docs/jax-rs.html), and [this](http://weblog4j.com/2012/03/15/developing-restful-services-using-apache-cxf/) tutorial. I use this json provider "org.apache.cxf.jaxrs.provider.json.JSONProvider". – Xstian Sep 24 '14 at 08:17
  • Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [META-INF/cxf/cxf.xml]; nested exception is java.io.FileNotFoundException: class path resource [META-INF/cxf/cxf.xml] cannot be opened because it does not exist - Any clue? – Rosh Sep 24 '14 at 12:14
  • Have you in your classpath this jar "cxf-rt-core-x.x.x.jar" ? – Xstian Sep 24 '14 at 12:22
  • Thank you worked. Sorry to disturb ... ERROR [org.springframework.web.context.ContextLoader] (ServerService Thread Pool -- 49) Context initialization failed: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://cxf.apache.org/jaxrs] ? Can you help in this ? – Rosh Sep 24 '14 at 12:32
  • Is probably that missing another jar .. "cxf-rt-frontend-jaxrs-x.x.x.jar" – Xstian Sep 24 '14 at 12:36
  • ERROR [org.springframework.web.context.ContextLoader] Context initialization failed:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cxf' defined in class path resource [META-INF/cxf/cxf.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.apache.cxf.bus.spring.SpringBus]: Constructor threw exception; nested exception is java.lang.LinkageError: Failed tolinkorg/apache/cxf/transport/http/policy/HTTPClientAssertionBuilder (Module ".war" from Service Module Loader) – Rosh Sep 24 '14 at 12:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61839/discussion-between-rosh-and-xstian). – Rosh Sep 24 '14 at 12:51