English 中文(简体)
JBoss Fuse – Rest Web Services
  • 时间:2024-12-27

JBoss Fuse - Rest Web Services


Previous Page Next Page  

To begin with, REST stands for Representational State Transfer. It is a way of developing web services based on state-less, cacheable, cpent-server protocol, which is HTTP in most cases.

REST web services use HTTP requests to post, get, delete data from network.

REST Development using CXF

Create a simple Maven quick-start project

mvn archetype:generate 
-DgroupId = com.tuts.abhinav 
-DartifactId = rest-service
-DarchetypeArtifactId = maven-archetype-quickstart 
-DinteractiveMode = false

Add dependencies

<dependency>
   <groupId>org.apache.servicemix.specs</groupId>
   <artifactId>org.apache.servicemix.specs.jsr311-api-1.1.1</artifactId>
   <version>1.9.0</version>
   <scope>provided</scope>
</dependency>

<dependency>
   <groupId>org.apache.servicemix</groupId>
   <artifactId>servicemix-http</artifactId>
   <version>2013.01</version>
</dependency>

<dependency>
   <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>1.2.16</version>
</dependency>

Add Build Instruction

<build>
   <defaultGoal>install</defaultGoal>
   <plugins>
      <plugin>
         <groupId>org.apache.fepx</groupId>
         <artifalctId>maven-bundle-plugin</artifactId>
         <version>2.3.4</version>
         <extensions>true</extensions>
			
         <configuration>
            <instructions>
               <Bundle-SymbopcName>rest-example-database-post-method
                  </Bundle-SymbopcName>
               <Import-Package>* </Import-Package>
            </instructions>
         </configuration>
			
      </plugin>
   </plugins>
</build>

Add Fuse Plugin Repositories

<pluginRepositories>
   <pluginRepository>
      <id>fusesource.m2</id>
      <name>FuseSource Community Release Repository</name>
      <url>http://repo.fusesource.com/nexus/content/repositories/releases</url>
      <snapshots>
         <enabled>false</enabled>
      </snapshots>
		
      <releases>
         <enabled>true</enabled>
      </releases>
   </pluginRepository>
<pluginRepositories>

Add Repositories

<repositories>
   <repository>
      <id>fusesource.m2</id>
      <name>FuseSource Community Release Repository</name>
      <url>http://repo.fusesource.com/nexus/content/repositories/releases</url>
      <snapshots>
         <enabled>false</enabled>
      </snapshots>
		
      <releases>
         <enabled>true</enabled>
      </releases>
		
   </repository>
	
   <repository>
      <id>fusesource.ea</id>
      <name>FuseSource Community Early Access Release Repository</name>
      <url>http://repo.fusesource.com/nexus/content/groups/ea</url>
      <snapshots>
         <enabled>false</enabled>
      </snapshots>
      <releases>
         <enabled>true</enabled>
      </releases>
   </repository>
	
</repositories>

Create Service Class

Create class UserService.java under com/tuts/

package com.tuts;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/UserService_1")
pubpc class UserService {
   @GET
   @Path("/get_data")
   @Produces(MediaType.APPLICATION_JSON)
	
   pubpc String getUser() {
      String reponse = "This is standard response from REST";
      return reponse;
   }
}

Create Blueprint.xml

Create blueprint.xml under/src/main/resources/OSGI-INF/blueprint blueprint.xml

<?xml version = "1.0" encoding = "UTF-8"?>
<blueprint xmlns = "http://www.osgi.org/xmlns/blueprint/v1.0.0"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 
   xmlns:jaxrs = "http://cxf.apache.org/blueprint/jaxrs"
   xsi:schemaLocation = "http://www.osgi.org/xmlns/blueprint/v1.0.0 
   http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
   http://cxf.apache.org/blueprint/jaxrs 
   http://cxf.apache.org/schemas/blueprint/jaxrs.xsd">
	
   <jaxrs:server id = "service" address = "/users">
      <jaxrs:serviceBeans>
         <ref component-id = "userService" />
      </jaxrs:serviceBeans>
   </jaxrs:server>
	
   <bean id = "userService" class = "com.tuts.UserService" />
</blueprint>

Install Rest service in Fuse

install -s mvn:com.tuts.abhinav/rest-service/1.0-SNAPSHOT

Check if Bundle has a Registered Web-Service

Open URL http://localhost:8181/cxf

Registered Web-Service

Test Web Service

Open URL http://localhost:8181/cxf/users12/UserService_1/get_data

Localhost CXF Advertisements