Exposing REST + JSON endpoint using WEF

I wanted to figure out how to create a REST + JSON end point using WEF. So i decided to create a simple Java class that has sayHello(name) method, this method takes name as input and when you invoke it it returns Hello name as output These are the steps that i followed
  • I did create a HelloWorldService.java class like this
    package com.webspherenotes.wef;
    public class HelloWorldService {
      public String sayHello(String name){
        System.out.println("Inside HelloWorldService.sayHello() Name = " + name);
        return "Hello " + name;
  • Next i did create one empty model and i did name it HelloWorldServiceProvider.model
  • THen in the HelloWorldServiceProvider.model i did add one Linked Java Object that points to HelloWorldService class like this
  • Define HelloWorldService using Service Definition builder like this
  • Now define sayHello() operation in the HelloWorldService using Service Operation builder like this
  • With this we have a Service Provider model, Now we need to consumer it and expose it as REST Service, For that create another empty model called HelloWorldRESTService.model
  • Add Service Consumer builder in the HelloWorldServiceConsumer.model like this
  • Add a REST Service Enable builder in this model and expose it as JSON like this
  • Now your application is ready so publish it on server
Next right click on the HelloWorldRESTService.model and run it as active model, you should get a web page like this
The HelloWorldService has only one operation sayHello so you get only one method listed here, select that method, you should get a page that lets you invoke the service
On this page enter a name and click on invoke service button and you should get JSON result like this

No comments: