Skip to content

Instantly share code, notes, and snippets.

@takawitter
Last active February 12, 2016 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takawitter/5a12059ef8ec971efa61 to your computer and use it in GitHub Desktop.
Save takawitter/5a12059ef8ec971efa61 to your computer and use it in GitHub Desktop.
The script which serve the service via JSON-RPC using Service Grid Service Container and Jetty.
groovy service.groovy
curl --globoff "http://127.0.0.1:8080/app/services/TestService?method=f&params=[3]&id=1"
curl "http://127.0.0.1:8080/app/services/TestService" -d '{"method":"f","params":[3],"id":1}'
@Grab('org.eclipse.jetty:jetty-webapp:9.3.0.M1')
@Grab('net.servicegrid:jp.go.nict.langrid.servicecontainer:1.0.6')
import jp.go.nict.langrid.servicecontainer.handler.annotation.Service;
import jp.go.nict.langrid.servicecontainer.handler.jsonrpc.servlet.JsonRpcServlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
public interface TestService{
int f(int n);
}
public class TestServiceImpl implements TestService{
public int f(int n){ return n + 2;}
}
@Service(name="TestService", impl=TestServiceImpl.class, intf=TestService.class)
public class Servlet extends JsonRpcServlet{}
WebAppContext webapp = new WebAppContext("./", "/app");
webapp.addServlet(Servlet.class, "/services/*");
Server server = new Server(8080);
server.setHandler(webapp);
server.start();
server.join();
/// <reference path="jquery.d.ts" />
class TestService{
constructor(path: string){
this.path = path;
}
public f(: number): number{
return this.ajax("f", []);
}
private ajax(methodName: string, params: any[]): any{
return $.ajax({
type: "POST", url: this.path,
dataType: "json", data: JSON.stringify({
method: methodName,
params: params
})
});
}
private path: string;
}
@takawitter
Copy link
Author

TestService.ts will be generated when you access http://127.0.0.1:8080/app/services/TestService?ts after you launched the service.

@takawitter
Copy link
Author

You can test the service using web browser by accessing http://127.0.0.1:8080/app/services .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment