Java Architecture for JSON Binding
JAJB provides the simplest, shortest way to call the server side java api from the front end javascript.
package foo;
import com.d_project.jajb.rpc.Callable;
public class MyService {
@Callable
public int add(int a, int b) { return a + b; }
}
/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
$.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 jquery-ajax-wrapper.js, call more simply.
jajb.getService('MyService')('add')(3, 5).done(function(data) {
console.log(JSON.stringify(data) );
});
package foo;
import com.d_project.jajb.JSONField;
import com.d_project.jajb.JSONType;
@JSONType
public class MyVO {
@JSONField
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"}}
Java Type | JSON Type |
---|---|
null (Object) | null (Object) |
boolean | Boolean |
Primitive Number Types | Number |
java.lang.Number | Number |
java.lang.String | String |
Array | Array |
java.util.Iterable | Array |
java.util.Map | Object |
POJO Object | Object |
JSON Type | Java Type |
---|---|
null (Object) | null (Object) |
Boolean | boolean |
Number | java.math.BigDecimal |
String | java.lang.String |
Array | java.util.ArrayList |
Object | java.util.LinkedHashMap |
JSON Type | Java Type |
---|---|
Number | Primitive Number Types, java.math.BigInteger |
Array | Array, java.util.ArrayList |
Object | POJO Object |