Request Routing to Business Method Design (1)-How far can we go series (43), how far 43
How far can we go from a series of regular web projects (43), such as spring MVC, to specify a url request to a method in a Controller, this method completes the specific business. We are not familiar with this, but we can also read the source code. Such routing tasks may also occur in actual projects. In fact, any framework that needs to process requests must solve this problem. The problem is thrown out first, and an example is used in subsequent articles to design a complete solution. It is worth studying to see how other frameworks solve this problem. Simplify this process: Let's do one thing, that is, how to arrange specific business methods for execution when processing requests. 1. Use a map to associate the key in the request to be used (assume that the json file contains a key to indicate the business) with the corresponding class. 2, use the template method to concatenate the three flows in the figure. The business processing subclass is implemented. each subclass has a doservice method 3. json is parsed during request processing and the key is obtained, then retrieve the specified instance from the map and execute the doservice method. The Code is as follows: assume that the request first reaches the executeOpenService method of RemoteOpenServiceImpl, and directly routes the service instance to be executed.
public interface RemoteOpenService { public JSON executeOpenService(JSON json, String method);}public class RemoteOpenServiceImpl implements RemoteOpenService{ @Override public JSON executeOpenService(JSON json, String method) { JSON returnJson = null; BaseService service = (BaseService) OpenServiceRouter.getRouterMap().get(method); returnJson = service.execute(json, ExampleRequest.class); return returnJson; } }
OpenServiceRouter:
public class OpenServiceRouter { private static Map<String, Object> routerMap; public static Map<String, Object> getRouterMap(){ if(routerMap != null){ return routerMap; } routerMap = new HashMap<String, Object>(); routerMap.put("example", "exampleService"); return routerMap; } }
Template Method Design: The excute method of the service is actually executed, and the implementation of the business part is left to the subclass implementation during the execution of this method.
Public interface BaseService {public JSON execute (JSON json, Class c);} public abstract class AbstractBaseService implements BaseService {// TODO public final Request decode (JSON json JSON, Class c) {ObjectMapper mapper = new ObjectMapper (); Request request = null; try {request = mapper. readValue (json. toJSONString (), c);} catch (JsonParseException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (JsonMappingException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} return request;} // TODO public final JSON encode (Response response) {ObjectMapper mapper = new ObjectMapper (); // Convert object to JSON string JSON json = null; try {String j = mapper. writeValueAsString (response); json = JSON. parseObject (j);} catch (JsonGenerationException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (JsonMappingException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} return json;}/*** different sub-classes implement specific logic * @ param request */public abstract Response doService (Request request ); /*** template execution Method * @ param json */public final JSON execute (JSON json, Class c) {Request request = this. decode (json, c); Response response = doService (request); JSON retrunJson = this. encode (response); return retrunJson ;}}
Subclass implementation:
public class ExampleServiceImpl extends AbstractBaseService{ @Override public Response doService(Request request) { ExampleRequest exampleRequest = (ExampleRequest)request; return null; }}
Because we just need to design such a called component, I think many places will encounter this scenario, so it is worth further research. The above is what I think, without reference to any framework, implement such a request route in the most primitive way.
As part of the Notes, let's start first and study the design of other frameworks on this issue.