Wednesday, May 13, 2015

JBoss FSW Switchyard Tutorial 2: REST Binding

Introduction

The source code for this tutorial is available at: https://github.com/garyliu1119/Switchyard-Tutorial/tree/master/switchyard-rest-demo-one

The video demo for this blog is available at youtube here now.

In my previous tutorial, I have covered the basics about Switchyard design, using the example of creating a Hello World Service. The link is available here:

The main purpose of this article is to document the basic procedures of the creation of a REST binding for Switchyard application. The 1.1 implementation still has some bugs. There are still quite some room for improvement in terms of simplicity and intuitiveness.

Create Order Service

Step One: Create Project

New --> Switchyard Project.
Project name: switchyard-rest, Next
Fill the form like the following:
click Finish

A new Switchyard project will be created. However, there is problem with pom.xml. This is a know bug for the version 1.1.1-p5-redhard-1. The workaround is to add a parent segment. Here is the complete pom.xml content:



  4.0.0
  
    org.switchyard
    switchyard-parent
    1.1.1-p5-redhat-1
  
  
  com.ggl.switchyard
  switchyard-rest
  com.ggl.switchyard:switchyard-rest
  
    1.1.1-p5-redhat-1
    1.7
    1.7
  
  
    
      org.switchyard.components
      switchyard-component-resteasy
    
    
      org.switchyard.components
      switchyard-component-soap
    
    
      org.switchyard
      switchyard-api
    
    
      org.switchyard
      switchyard-transform
    
    
      org.switchyard
      switchyard-validate
      ${switchyard.version}
    
    
      org.switchyard
      switchyard-test
      test
    
    
      org.switchyard.components
      switchyard-component-test-mixin-cdi
      test
    
  
  
    
      
        org.switchyard
        switchyard-plugin
        
          
            
              configure
            
          
        
        
          
            org.switchyard.transform.config.model.TransformSwitchYardScanner
          
        
      
    
  


This is pretty nasty work around. Your project version has to be the same as the switchyard version. As I know this is fixed in the newer version of switchyard. I will verify that soon.

We need to update the maven project as the pom.xml file is modified [using Option-F5 in MacOS].

Create OrderService

This is the same as my previous HelloWorld service, exception the interface name is OrderService and the bean name is OrderServiceImpl.

Create REST Binding

Switchyard for REST service require a resource file, which is an interface definition. In my case, I have defined as the following:

package com.ggl.switchyard.switchyard_rest_demo;

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

@Path("/order")
public interface OrderResource {

    @POST
    @Path("/")
    @Produces({"text/xml"})
    public Order newOrder();

    @GET
    @Path("{orderId}")
    @Produces({"text/xml"})
    public Order getOrder(@PathParam("orderId") Long orderId);

}

This interface must be the same as the implementation service interface. In this tutorial, I have defined the OrderService as the following:

public interface OrderService {
 public Order newOrder (Order order);
 public Order getOrder (Long orderId);
}

As you can see that the signature is the same if you remove the annotations from the resource interface. This seems a bit awkward. To me there should be an implementation for the REST service explicitly, like in Spring MVC, of Apache CXF

Summary

4 comments:

  1. Hello, I read your post and I have a problem, wanting to create a maven project with swichyard, created an archetype, but as I make the call from my project maven fuse

    ReplyDelete
  2. My issue is "The referenced java interface for service 'OrderService-REST' specifies more than one operation and the binding 'OrderBinding' does specifies which to use in the operation selector".

    Please suggest me a approach.

    ReplyDelete
  3. Pleease help me how to do CRUD operation using RestAPI in switchyard & how to do bulk update also.Thanks in advance

    ReplyDelete
  4. Can u share the series of youtube links for switchyard where you have explained all the components and bindings. I am getting only Tutorial 1,2 and 3 only.

    ReplyDelete

Anypoint Studio Error: The project is missing Munit lIbrary to run tests

Anypoint Studio 7.9 has a bug. Even if we following the article: https://help.mulesoft.com/s/article/The-project-is-missing-MUnit-libraries-...