MoinMoin Logo
  • Comments
  • Immutable Page
  • Menu
    • Navigation
    • RecentChanges
    • FindPage
    • Local Site Map
    • Help
    • HelpContents
    • HelpOnMoinWikiSyntax
    • Display
    • Attachments
    • Info
    • Raw Text
    • Print View
    • Edit
    • Load
    • Save
  • Login

Navigation

  • Start
  • Sitemap

Upload page content

You can upload content for the page named below. If you change the page name, you can also upload content for another page. If the page name is empty, we derive the page name from the file name.

File to load page content from
Page name
Comment

Revision 7 as of 2015-03-17 20:51:49
  • Java
  • ApacheCXF

Java/ApacheCXF

http://cxf.apache.org/

Apache CXF is an open source services framework. CXF helps you build and develop services using frontend programming APIs, like JAX-WS and JAX-RS. These services can speak a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, or CORBA and work over a variety of transports such as HTTP, JMS or JBI.

Sample SOAP Web Service

Structure:

.
|-- pom.xml
|-- src
|   `-- main
|       |-- java
|       |   `-- com
|       |       `-- test
|       |           |-- Calculator.java
|       |           |-- ICalculator.java
|       |           `-- TestService.java
|       `-- webapp
|           `-- WEB-INF
|               |-- applicationContext.xml
|               `-- web.xml
`-- target
  • mkdir -p /tmp/cxfSpringTest
  • cd /tmp/cxfSpringTest
  • mkdir -p src/main/webapp/WEB-INF/
  • mkdir -p src/main/java/com/test/
  • nano pom.xml

   1 <project
   2 xmlns="http://maven.apache.org/POM/4.0.0"
   3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   5   <modelVersion>4.0.0</modelVersion>
   6   <groupId>com.test</groupId>
   7   <artifactId>cxfSpringTest</artifactId>
   8   <version>0.1</version>
   9   <packaging>war</packaging>
  10   <dependencies>
  11     <dependency>
  12        <groupId>org.apache.cxf</groupId>
  13        <artifactId>cxf-rt-core</artifactId>
  14        <version>2.4.1</version>
  15     </dependency>
  16     <dependency>
  17        <groupId>org.apache.cxf</groupId>
  18        <artifactId>cxf-bundle-jaxrs</artifactId>
  19        <version>2.4.1</version>
  20     </dependency>
  21     <dependency>
  22        <groupId>org.apache.cxf</groupId>
  23        <artifactId>cxf-rt-frontend-jaxws</artifactId>
  24        <version>2.4.1</version>
  25     </dependency>
  26   </dependencies>
  27 </project>
  • nano src/main/java/com/test/Calculator.java

   1 package com.test;
   2 
   3 public class Calculator implements ICalculator
   4 {
   5     public Calculator() {
   6         System.out.println("Calculator created ");
   7     }
   8 
   9     public long add(long num1, long num2) {
  10         return (num1 + num2);
  11     }
  12 
  13     public long subtract( long num1, long num2  ){
  14         return num1 - num2;
  15     }
  16 }
  • nano src/main/java/com/test/ICalculator.java

   1 package com.test;
   2 import javax.jws.WebService;
   3 
   4 @WebService
   5 public interface ICalculator {
   6     public long add(long num1 , long num2 );
   7     public long subtract(long num1, long num2 );
   8 }
  • nano src/main/webapp/WEB-INF/applicationContext.xml

   1 <beans xmlns="http://www.springframework.org/schema/beans"
   2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   3 xmlns:jaxws="http://cxf.apache.org/jaxws"
   4 xmlns:jaxrs="http://cxf.apache.org/jaxrs"
   5 xsi:schemaLocation=" http://www.springframework.org/schema/beans
   6 http://www.springframework.org/schema/beans/spring-beans.xsd
   7 http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
   8 http://cxf.apache.org/jaxrs
   9 http://cxf.apache.org/schemas/jaxrs.xsd
  10 ">
  11     <import resource="classpath:META-INF/cxf/cxf.xml"/>
  12     <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
  13     <!-- jax-ws -->
  14     <bean id="calculatorService" class="com.test.Calculator" scope="singleton" />
  15     <jaxws:endpoint implementor="#calculatorService" address="/calculator"  />
  16     <!-- jax-rs -->
  17     <jaxrs:server id="testService" address="/">
  18         <jaxrs:serviceBeans>
  19             <ref bean="testService" />
  20         </jaxrs:serviceBeans>
  21     </jaxrs:server>
  22     <bean id="testService" class="com.test.TestService" />
  23 </beans>
  • nano src/main/webapp/WEB-INF/web.xml

   1 <web-app>
   2   <!-- <context-param>
   3         <param-name>contextConfigLocation</param-name>
   4         <param-value>/WEB-INF/applicationContext.xml</param-value>
   5   </context-param>  default location for spring context  -->
   6   <servlet>
   7     <servlet-name>CXFServlet</servlet-name>
   8     <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
   9   </servlet>
  10   <servlet-mapping>
  11     <servlet-name>CXFServlet</servlet-name>
  12     <url-pattern>/*</url-pattern>
  13   </servlet-mapping>
  14   <listener>
  15     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  16   </listener>
  17   <servlet>
  18     <servlet-name>Spring</servlet-name>
  19     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  20     <!-- <init-param>
  21         <param-name>contextConfigLocation</param-name>
  22         <param-value>/WEB-INF/applicationContext.xml</param-value>
  23     </init-param> -->
  24     <load-on-startup>0</load-on-startup>
  25   </servlet>
  26 </web-app>
  • nano src/main/java/com/test/TestService.java

   1 package com.test;
   2 import javax.ws.rs.GET;
   3 import javax.ws.rs.Path;
   4 import javax.ws.rs.PathParam;
   5 import javax.ws.rs.core.Response;
   6 
   7 @Path("/testSvc")
   8 public class TestService {
   9 
  10     @GET
  11     @Path("/{param}")
  12     public Response getMsg(@PathParam("param") String msg) {
  13         String out = String.format("testSvc returns %s", msg);
  14         return Response.status(200).entity(out).build();
  15     }
  16 
  17 }

mvn clean compile package # builds cxfSpringTest-0.1.war

Test links:

  • http://localhost:8080/cxfSpringTest-0.1/services

  • http://localhost:8080/cxfSpringTest-0.1/testSvc/1001

  • MoinMoin Powered
  • Python Powered
  • GPL licensed
  • Valid HTML 4.01