Вы находитесь на странице: 1из 18

Steps to create RESTFul web service using Apache-CXF with the help of JAX-RS

1. Tomcat

2. Eclipse Helios

File->New->Dynamic Web Project


Click on Finish button

Expand Demo and open web.xml file


Add these lines as above and save web.xml file
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/services.xml</param-value>
</context-param>
<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>/*</url-pattern>
</servlet-mapping>

<param-value>WEB-INF/services.xml</param-value>
See the above line
So we will create a new file in WEB-INF and save it as services.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:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd"
default-lazy-init="false">

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


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

<jaxrs:server id="DemoService" address="/DemoService" >


<jaxrs:serviceBeans>
<bean class="org.demo.services.DemoService" />
</jaxrs:serviceBeans>
</jaxrs:server>

</beans>

Store as services.xml in WEB-INF

Note the above line in services.xml file. It needs a class called DemoService
in org.demo.service package.

Now we will built the class file in src folder.

Click on Project Explore Tab


Put the Package: org.demo.service and Name: DemoService

Click Finish
Add 2 simple methods to the class with @GET Request Method designator
package org.demo.services;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

public class DemoService {

@GET
@Path("/version")
@Produces("text/plain")
public String getVersion(){
return "3.0";
}

@GET
@Path("/scope")
@Produces("application/xml")
public String getScope(){
return "<scopeName>Scope-1</scopeName>";
}
}
Ohh lots of error as we have not included any libraries yet 

Let’s add the CXF library. Right click on the Demo folder and click on Properties at the bottom
Click on Add Library…. Button on right side
Select User Library

Next
Ok

Select CXF2-3-0 and Click on Add Jars on right side


I have all jar files inside a folder that I have downloaded from apach-cxf site. Select all jar files then OPEN
Click on Finish->OK

See errors gone but 1 moment these jar are not added to your server Tomcat so Click on Markers Tab
Click on Finish button
Now you are ready to run the application
Click on Finish
http://localhost:8080/Demo/DemoService/version

http://localhost:8080/Demo/DemoService/scope

Вам также может понравиться