Java Architecture for JSON Binding
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.settings commit Sep 13, 2018
src separate props. Sep 14, 2018
.classpath commit Sep 13, 2018
.gitignore enmaven Sep 12, 2018
.project enmaven Sep 12, 2018
LICENSE license Sep 13, 2018
README.md Merge remote-tracking branch 'origin/master' Sep 14, 2018
pom.xml fix some. Sep 17, 2018

README.md

JAJB

Java Architecture for JSON Binding

JAJB provides the simplest, shortest way to call the server side java api from the front end javascript.

First step

  1. Create a POJO Service.
package foo;

import com.d_project.jajb.rpc.Callable;

public class MyService {
  @Callable
  public int add(int a, int b) { return a + b; }
}
  1. Setup a RPCServlet.

/WEB-INF/web.xml

  <servlet>
    <servlet-name>jajb-rpc</servlet-name>
    <servlet-class>com.d_project.jajb.rpc.RPCServlet</servlet-class>
    <init-param>
      <param-name>services</param-name>
      <param-value>/WEB-INF/services.properties</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>jajb-rpc</servlet-name>
    <url-pattern>/jajb-rpc</url-pattern>
  </servlet-mapping>

/WEB-INF/services.properties

MyService=foo.MyService
  1. Call from a client (with jQuery).
  $.ajax({
    url : 'jajb-rpc',
    method : 'POST',
    type : 'application/json',
    data : JSON.stringify([
      { serviceName : 'MyService', methodName : 'add' }, // opts
      [ 3, 5 ] // args
    ])
  }).done(function(data) {
    console.log(JSON.stringify(data) );
  });

Here is a result.

{"status":"success","result":8}

With POJO VO (Value Object)

package foo;

import com.d_project.jajb.JSONSerializable;

@JSONSerializable
public class MyVO {

  @JSONSerializable
  private String message;
  public String getMessage() { return message; }
  public void setMessage(String message) { this.message = message; }

  // This property will not be serialized.
  private String notSerializable;
  public String getNotSerializable() { return notSerializable; }
  public void setNotSerializable(String notSerializable) {
    this.notSerializable = notSerializable;
  }
}
package foo;

import com.d_project.jajb.rpc.Callable;

public class MyVOService {
  @Callable
  public MyVO helloVO(MyVO vo) {
    vo.setMessage("hello," + vo.getMessage() +
        "," + vo.getNotSerializable() );
    vo.setNotSerializable("Can you hear me?");
    return vo;
  }
}
  $.ajax({
    url : 'jajb-rpc',
    method : 'POST',
    type : 'application/json',
    data : JSON.stringify([
      { serviceName : 'MyVOService', methodName : 'helloVO' }, // opts
      [ { message : 'abc', notSerializable : 'will be ignored' } ] // args
    ])
  }).done(function(data) {
    console.log(JSON.stringify(data) );
  });

Here is a result.

{"status":"success","result":{"message":"hello,abc,null"}}