Never give up on yourself, even when everyone else does

1. Since living yu heap cements 2. HttpRequestHandlerAdapterSimpleControllerHandlerAdapterSimpleServletHandlerAdapterRequestMappi HandlerAdapter implementation ngHandlerAdapter3. Conclusion:

1. I don’t know why I was born bright

From the previous articles, we learned that we can get the handler we need from HandlerMapping based on the URL. With handler, we can handle programs.

Why do WE need a HandlerAdapter with HandlerMapping?

Let’s look at the problem between DispatcherServlet#doDispatch(request, response) and handler

  1. The Servlet has only two input parameters, but the handler has a variety of input parameters. In particular, @requestMapping is a common way to define parameters that depend on the developer’s mood
  2. Handler returns a variety of types, returning data types in various formats.
  3. There are four ways to define handler.

This fixed Servlet specification conflicts with the flexible Handler.

So the great design pattern comes into play, and the adapter pattern exists to solve this incompatibility on both sides. The HandlerAdapter is this adapter

The adapter will handle the complex processing of the return value. The adapter will handle the selection of the Handler

This also provides a good template for using the adapter pattern.

public interface HandlerAdapter {



    boolean supports(Object handler);



    ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;



    long getLastModified(HttpServletRequest request, Object handler);

}

Copy the code

Three methods are defined in the HandlerAdapter:

  • Supports: Determines whether the current adapter supports adaptation of the current handler. So that’s the Handler selection problem
  • Handle () : Actually calls Handler, the core of the adaptation. Help us solve the parameters, return values and a number of peripheral issues
  • GetLastModified () : Gets the last change time of the current request, which is used by the browser to determine whether the current request has been modified and therefore whether the previously cached result can be used directly

2. HandlerAdapter implementation

The HandlerAdapter implementation is different for different handlers.

HttpRequestHandlerAdapter
public class HttpRequestHandlerAdapter implements HandlerAdapter {



    @Override

    public boolean supports(Object handler) {

        return (handler instanceof HttpRequestHandler);

    }



    @Override

    public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)

            throws Exception 
{



        ((HttpRequestHandler) handler).handleRequest(request, response);

        return null;

    }



    @Override

    public long getLastModified(HttpServletRequest request, Object handler) {

        if (handler instanceof LastModified) {

            return ((LastModified) handler).getLastModified(request);

        }

        return -1L;

    }



}

Copy the code

See HttpRequestHandlerAdapter. Supports to determine whether its HttpRequestHandler interface type, Also said HttpRequestHandlerAdapter adapter org. Springframework. Web. HttpRequestHandler this Handler

The Handle method directly calls the handleRequest method of the HttpRequestHandler instance

SimpleControllerHandlerAdapter

The Supports method implements the Controller interface handler. The Handle method also calls the handleRequest method of the Controller instance directly

public class SimpleControllerHandlerAdapter implements HandlerAdapter {



    @Override

    public boolean supports(Object handler) {

        return (handler instanceof Controller);

    }

    @Override

    public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)

            throws Exception 
{



        return ((Controller) handler).handleRequest(request, response);

    }

}

Copy the code
SimpleServletHandlerAdapter

The Supports method ADAPTS to a Servlet handler, and the Handle method directly calls the Service method of the Servlet instance

public class SimpleServletHandlerAdapter implements HandlerAdapter {



    @Override

    public boolean supports(Object handler) {

        return (handler instanceof Servlet);

    }

    @Override

    public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)

            throws Exception 
{



        ((Servlet) handler).service(request, response);

        return null;

    }

}

Copy the code
RequestMappingHandlerAdapter

The most important HandlerAdapter, important because it is the most complex and one of the core features of SpringMVC.

Look at the other three handler adapters above. It’s all very simple. The key for SpringMVC adapters are in RequestMappingHandlerAdapter

Now let’s look at its supports method, actually in the superclass AbstractHandlerMethodAdapter now

public abstract class AbstractHandlerMethodAdapter {

   public final boolean supports(Object handler) {

           return (handler instanceof HandlerMethod && supportsInternal((HandlerMethod) handler));

       } 

}

public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter{

   protected boolean supportsInternal(HandlerMethod handlerMethod) {

           return true;

       } 

}

Copy the code

Handler is of type HandlerMethod,

Handle method of implementation in his father’s class AbstractHandlerMethodAdapter, a template method, called subclass RequestMappingHandlerAdapter# handleInternal method

public abstract class AbstractHandlerMethodAdapter extends WebContentGenerator implements HandlerAdapter.Ordered {



    public final ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler)

            throws Exception 
{



        return handleInternal(request, response, (HandlerMethod) handler);

    }

}

Copy the code

RequestMappingHandlerAdapter# handle method for SpringMVC is the most complicated one. The matching of handler input parameters, handling of return values, and other operations mentioned above are reflected in this adaptation.

RequestMappingHandlerAdapter# handle method is more complex, the working principle of single open an article about it.

3. Conclusion:

The HandlerAdapter acts as a bridge between servlets and a variety of handlers, taking care of parameter matching, return value handling, and so on. Compatibility is the job of the HandlerAdapter.

So:

  • HandlerMapping, which resolves registration handlers
  • HandlerAdapter, responsible for compatibility calls

You need both HandlerMapping and HandlerAdapter


If you find this article useful, please like 👍 please follow ❤️ please share 👥 if there are any mistakes in this article, please comment, thank you very much!

Micro channel public number: [source action], plus I know know bai I write their own article positioning is personal understanding diary. Because whenever I write an article, I will focus on the understanding and elaboration of knowledge points, which will deepen and strengthen the understanding of knowledge points.

In the New Year, I enjoy reading the source code, not technology to improve rapidly, but for comfort.