Many services have expose some specific JSON messages as the API protocol.
-
One service/client send a JSON message to another service for RPC.
-
The other serice receives a JSON message to invoke some specific logic inside the service.
To avoid parse the JSON message each time manually:
-
Create a framework that can parse the JSON message according the some specific model.
-
The framework can parse the JSON message, and invoke a specific logic inside the service.
-
Implement something just the Spring annotation framework(@Controller, @Component, etc).
- Message model definition:
In common designs, the message contain two parts: Header and Body.
--The Header contains the information like 'requestId', 'sourceId', 'originalId' which are used to identify the caller, the source and sender.
--The Body contains the message data, that the service will used to invoke a specific logic.
So, the lib provided the annotations below for model definition:
@APIComponentDefinition, @APIDefinition, @APIParametersDefinition, @APIParameterDefinition, @APIHeaderDefinition
A sample message model might be:
public class MessageModel {
@APIHeaderDefinition
private String requestId;
private MessageBody body;
// getters for each field
private class MessageBody {
@APIComponentDefinition
private String command;
private MessageContent content;
// getters for each field
private class MessageContent {
@APIDefinition
private String actionName;
@APIParametersDefinition
private Map<String, Object> paramterMap;
@APIParametersDefinition
private ParametersSampeModel paramterModel;
@APIParameterDefinition
private SingleParameterModel singleParameterModel;
// getters for the fields
}
}
}
- Invoke strategy:
-
The framework will parse the message model, and find the @APIComponentDefintion, @APIDefinition, @APIParametersDefintion, etc.
-
Then it will scan a basepackage to find the compoents with the annotation: @APIComponent,
-
It will invoke the most match @API, inside each @APICompoent.
Check a details demo under the package **.Sample
