Class WebMvcUtils

  • All Implemented Interfaces:
    io.microsphere.util.Utils

    public abstract class WebMvcUtils
    extends java.lang.Object
    implements io.microsphere.util.Utils
    Spring Web MVC Utilities Class
    Since:
    1.0.0
    Author:
    Mercy
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String INIT_PARAM_DELIMITERS
      Any number of these characters are considered delimiters between multiple values in a single init-param String value.
      static java.util.Set<java.lang.Class<? extends org.springframework.http.converter.HttpMessageConverter<?>>> SUPPORTED_CONVERTER_TYPES
      The supported HttpMessageConverter types
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      protected static java.lang.String[] getClassNames​(java.lang.Class<?>... classes)  
      static java.lang.Object[] getHandlerMethodArguments​(java.lang.reflect.Method method)
      Gets the HandlerMethod method parameter
      static java.lang.Object[] getHandlerMethodArguments​(javax.servlet.http.HttpServletRequest request, java.lang.reflect.Method method)
      Gets the HandlerMethod method parameter
      static java.lang.Object[] getHandlerMethodArguments​(javax.servlet.http.HttpServletRequest request, org.springframework.web.method.HandlerMethod handlerMethod)
      Gets the HandlerMethod method parameter
      static java.lang.Object[] getHandlerMethodArguments​(org.springframework.web.method.HandlerMethod handlerMethod)
      Gets the HandlerMethod method parameter
      static <T> T getHandlerMethodRequestBodyArgument​(java.lang.reflect.Method method)
      Gets the @RequestBody method parameter from the HttpServletRequest context
      static <T> T getHandlerMethodRequestBodyArgument​(javax.servlet.http.HttpServletRequest request, java.lang.reflect.Method method)
      Gets the @RequestBody method parameter from the HttpServletRequest context
      static <T> T getHandlerMethodRequestBodyArgument​(javax.servlet.http.HttpServletRequest request, org.springframework.web.method.HandlerMethod handlerMethod)
      Gets the @RequestBody method parameter from the HttpServletRequest context
      static <T> T getHandlerMethodRequestBodyArgument​(org.springframework.web.method.HandlerMethod handlerMethod)
      Gets the @RequestBody method parameter from the HttpServletRequest context
      static <T> T getHandlerMethodReturnValue​(java.lang.reflect.Method method)
      Gets the value returned by the HandlerMethod method
      static <T> T getHandlerMethodReturnValue​(javax.servlet.http.HttpServletRequest request, java.lang.reflect.Method method)
      Gets the value returned by the HandlerMethod method
      static <T> T getHandlerMethodReturnValue​(javax.servlet.http.HttpServletRequest request, org.springframework.web.method.HandlerMethod handlerMethod)
      Gets the value returned by the HandlerMethod method
      static <T> T getHandlerMethodReturnValue​(org.springframework.web.method.HandlerMethod handlerMethod)  
      static javax.servlet.http.HttpServletRequest getHttpServletRequest()
      Gets the current HttpServletRequest object
      static javax.servlet.http.HttpServletRequest getHttpServletRequest​(org.springframework.web.context.request.RequestAttributes requestAttributes)  
      static org.springframework.web.context.WebApplicationContext getWebApplicationContext()
      Gets the WebApplicationContext associated with the current Servlet Request request
      static org.springframework.web.context.WebApplicationContext getWebApplicationContext​(javax.servlet.http.HttpServletRequest request, javax.servlet.ServletContext servletContext)
      Get the WebApplicationContext from HttpServletRequest
      static boolean isControllerAdviceBeanType​(java.lang.Class<?> beanType)
      Determine whether the Bean Type is present annotated by ControllerAdvice
      static boolean isPageRenderRequest​(org.springframework.web.servlet.ModelAndView modelAndView)
      Is page render request
      static void setContextInitializerClassInitParameter​(javax.servlet.ServletContext servletContext, java.lang.Class<? extends org.springframework.context.ApplicationContextInitializer>... contextInitializerClasses)
      Sets the initialized parameter for Context Initializer Class
      static void setFrameworkServletContextInitializerClassInitParameter​(javax.servlet.ServletContext servletContext, java.lang.Class<? extends org.springframework.context.ApplicationContextInitializer>... contextInitializerClasses)
      Sets initialized parameter for Context Initializer Class into FrameworkServlet
      static void setGlobalInitializerClassInitParameter​(javax.servlet.ServletContext servletContext, java.lang.Class<? extends org.springframework.context.ApplicationContextInitializer>... contextInitializerClasses)
      Sets the initialized parameter for Global Initializer Class
      static void setHandlerMethodRequestBodyArgument​(java.lang.reflect.Method method, java.lang.Object requestBodyArgument)
      Set the @RequestBody method parameter in HandlerMethod to the HttpServletRequest context
      static void setHandlerMethodRequestBodyArgument​(javax.servlet.http.HttpServletRequest request, java.lang.reflect.Method method, java.lang.Object requestBodyArgument)
      Set the @RequestBody method parameter in HandlerMethod to the HttpServletRequest context
      static void setHandlerMethodReturnValue​(java.lang.reflect.Method method, java.lang.Object returnValue)
      Set the return value of the HandlerMethod to the HttpServletRequest context
      static void setHandlerMethodReturnValue​(javax.servlet.http.HttpServletRequest request, java.lang.reflect.Method method, java.lang.Object returnValue)
      Set the return value of the HandlerMethod to the HttpServletRequest context
      static void setInitParameters​(javax.servlet.ServletContext servletContext, java.lang.String parameterName, java.lang.String... parameterValues)
      Sets ServletContext Intialized Parameters
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • SUPPORTED_CONVERTER_TYPES

        public static final java.util.Set<java.lang.Class<? extends org.springframework.http.converter.HttpMessageConverter<?>>> SUPPORTED_CONVERTER_TYPES
        The supported HttpMessageConverter types
      • INIT_PARAM_DELIMITERS

        public static final java.lang.String INIT_PARAM_DELIMITERS
        Any number of these characters are considered delimiters between multiple values in a single init-param String value.
        See Also:
        ContextLoader.INIT_PARAM_DELIMITERS, Constant Field Values
    • Method Detail

      • getHttpServletRequest

        @Nullable
        public static javax.servlet.http.HttpServletRequest getHttpServletRequest()
                                                                           throws java.lang.IllegalStateException
        Gets the current HttpServletRequest object

        By default, HttpServletRequest is initialized in RequestContextFilter, HttpServletRequest from the Servlet HTTP request thread ThreadLocal is obtained from InheritableThreadLocal and can be obtained in the child thread.

        Returns:
        null returns the current HttpServletRequest object.
        Throws:
        java.lang.IllegalStateException
      • getHttpServletRequest

        @Nullable
        public static javax.servlet.http.HttpServletRequest getHttpServletRequest​(org.springframework.web.context.request.RequestAttributes requestAttributes)
      • getWebApplicationContext

        @Nonnull
        public static org.springframework.web.context.WebApplicationContext getWebApplicationContext()
                                                                                              throws java.lang.IllegalStateException
        Gets the WebApplicationContext associated with the current Servlet Request request
        Returns:
        Current Servlet Request associated with WebApplicationContext
        Throws:
        java.lang.IllegalStateException - In a non-Web scenario, an exception is thrown
      • getWebApplicationContext

        @Nullable
        public static org.springframework.web.context.WebApplicationContext getWebApplicationContext​(javax.servlet.http.HttpServletRequest request,
                                                                                                     @Nullable
                                                                                                     javax.servlet.ServletContext servletContext)
        Get the WebApplicationContext from HttpServletRequest
        Parameters:
        request - HttpServletRequest
        servletContext - ServletContext
        Returns:
        WebApplicationContext
        Throws:
        java.lang.IllegalStateException - if no servlet-specific context has been found
        See Also:
        RequestContextUtils#getWebApplicationContext(HttpServletRequest), RequestContextUtils.findWebApplicationContext(HttpServletRequest, ServletContext), DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE
      • setHandlerMethodRequestBodyArgument

        public static void setHandlerMethodRequestBodyArgument​(java.lang.reflect.Method method,
                                                               java.lang.Object requestBodyArgument)
        Set the @RequestBody method parameter in HandlerMethod to the HttpServletRequest context
        Parameters:
        method - Handler Method
        requestBodyArgument - @RequestBody The method parameters
      • setHandlerMethodRequestBodyArgument

        public static void setHandlerMethodRequestBodyArgument​(javax.servlet.http.HttpServletRequest request,
                                                               java.lang.reflect.Method method,
                                                               java.lang.Object requestBodyArgument)
        Set the @RequestBody method parameter in HandlerMethod to the HttpServletRequest context
        Parameters:
        request - HttpServletRequest
        method - Handler Method
        requestBodyArgument - @RequestBody The method parameters
      • setHandlerMethodReturnValue

        public static void setHandlerMethodReturnValue​(java.lang.reflect.Method method,
                                                       java.lang.Object returnValue)
        Set the return value of the HandlerMethod to the HttpServletRequest context
        Parameters:
        method - Handler Method
        returnValue - The return value
      • setHandlerMethodReturnValue

        public static void setHandlerMethodReturnValue​(javax.servlet.http.HttpServletRequest request,
                                                       java.lang.reflect.Method method,
                                                       java.lang.Object returnValue)
        Set the return value of the HandlerMethod to the HttpServletRequest context
        Parameters:
        request - HttpServletRequest
        method - Handler Method
        returnValue - The return value
      • getHandlerMethodRequestBodyArgument

        @Nullable
        public static <T> T getHandlerMethodRequestBodyArgument​(org.springframework.web.method.HandlerMethod handlerMethod)
        Gets the @RequestBody method parameter from the HttpServletRequest context
        Type Parameters:
        T - @RequestBody Method parameter Types
        Parameters:
        handlerMethod - HandlerMethod
        Returns:
        @RequestBody Method parameters if present, otherwise,null
      • getHandlerMethodRequestBodyArgument

        @Nullable
        public static <T> T getHandlerMethodRequestBodyArgument​(javax.servlet.http.HttpServletRequest request,
                                                                org.springframework.web.method.HandlerMethod handlerMethod)
        Gets the @RequestBody method parameter from the HttpServletRequest context
        Type Parameters:
        T - @RequestBody Method parameter Types
        Parameters:
        request - HttpServletRequest
        handlerMethod - HandlerMethod
        Returns:
        @RequestBody Method parameters if present, otherwise,null
      • getHandlerMethodRequestBodyArgument

        @Nonnull
        public static <T> T getHandlerMethodRequestBodyArgument​(java.lang.reflect.Method method)
        Gets the @RequestBody method parameter from the HttpServletRequest context
        Type Parameters:
        T - @RequestBody Method parameter Types
        Parameters:
        method - Handler Method
        Returns:
        @RequestBody method parameter if present, otherwise null
      • getHandlerMethodRequestBodyArgument

        @Nonnull
        public static <T> T getHandlerMethodRequestBodyArgument​(javax.servlet.http.HttpServletRequest request,
                                                                java.lang.reflect.Method method)
        Gets the @RequestBody method parameter from the HttpServletRequest context
        Type Parameters:
        T - @RequestBody Method parameter Types
        Parameters:
        request - HttpServletRequest
        method - Handler Method
        Returns:
        @RequestBody method parameter if present, otherwise null
      • getHandlerMethodArguments

        @Nonnull
        public static java.lang.Object[] getHandlerMethodArguments​(org.springframework.web.method.HandlerMethod handlerMethod)
        Gets the HandlerMethod method parameter
        Parameters:
        handlerMethod - HandlerMethod
        Returns:
        non-null
      • getHandlerMethodArguments

        @Nonnull
        public static java.lang.Object[] getHandlerMethodArguments​(javax.servlet.http.HttpServletRequest request,
                                                                   org.springframework.web.method.HandlerMethod handlerMethod)
        Gets the HandlerMethod method parameter
        Parameters:
        request - HttpServletRequest
        handlerMethod - HandlerMethod
        Returns:
        non-null
      • getHandlerMethodArguments

        @Nonnull
        public static java.lang.Object[] getHandlerMethodArguments​(java.lang.reflect.Method method)
        Gets the HandlerMethod method parameter
        Parameters:
        method - HandlerMethod
        Returns:
        non-null
      • getHandlerMethodArguments

        @Nonnull
        public static java.lang.Object[] getHandlerMethodArguments​(javax.servlet.http.HttpServletRequest request,
                                                                   java.lang.reflect.Method method)
        Gets the HandlerMethod method parameter
        Parameters:
        request - HttpServletRequest
        method - Method
        Returns:
        non-null
      • getHandlerMethodReturnValue

        @Nullable
        public static <T> T getHandlerMethodReturnValue​(org.springframework.web.method.HandlerMethod handlerMethod)
      • getHandlerMethodReturnValue

        @Nullable
        public static <T> T getHandlerMethodReturnValue​(javax.servlet.http.HttpServletRequest request,
                                                        org.springframework.web.method.HandlerMethod handlerMethod)
        Gets the value returned by the HandlerMethod method
        Type Parameters:
        T - Method return value type
        Parameters:
        request - HttpServletRequest
        handlerMethod - HandlerMethod
        Returns:
        HandlerMethod Method return value
      • getHandlerMethodReturnValue

        @Nullable
        public static <T> T getHandlerMethodReturnValue​(java.lang.reflect.Method method)
        Gets the value returned by the HandlerMethod method
        Type Parameters:
        T - Method return value type
        Parameters:
        method - Method
        Returns:
        HandlerMethod Method return value
      • getHandlerMethodReturnValue

        @Nullable
        public static <T> T getHandlerMethodReturnValue​(javax.servlet.http.HttpServletRequest request,
                                                        java.lang.reflect.Method method)
        Gets the value returned by the HandlerMethod method
        Type Parameters:
        T - Method return value type
        Parameters:
        request - HttpServletRequest
        method - Method
        Returns:
        HandlerMethod Method return value
      • isControllerAdviceBeanType

        public static boolean isControllerAdviceBeanType​(java.lang.Class<?> beanType)
        Determine whether the Bean Type is present annotated by ControllerAdvice
        Parameters:
        beanType - Bean Type
        Returns:
        If ControllerAdvice bean type is present , return true , or false.
      • setInitParameters

        public static void setInitParameters​(javax.servlet.ServletContext servletContext,
                                             java.lang.String parameterName,
                                             java.lang.String... parameterValues)
        Sets ServletContext Intialized Parameters
        Parameters:
        servletContext - ServletContext
        parameterName - the name of init parameter
        parameterValues - the values of init parameters
      • setGlobalInitializerClassInitParameter

        public static void setGlobalInitializerClassInitParameter​(javax.servlet.ServletContext servletContext,
                                                                  java.lang.Class<? extends org.springframework.context.ApplicationContextInitializer>... contextInitializerClasses)
        Sets the initialized parameter for Global Initializer Class
        Parameters:
        servletContext - ServletContext
        contextInitializerClasses - the classes of ApplicationContextInitializer
        See Also:
        ContextLoader.GLOBAL_INITIALIZER_CLASSES_PARAM
      • setContextInitializerClassInitParameter

        public static void setContextInitializerClassInitParameter​(javax.servlet.ServletContext servletContext,
                                                                   java.lang.Class<? extends org.springframework.context.ApplicationContextInitializer>... contextInitializerClasses)
        Sets the initialized parameter for Context Initializer Class
        Parameters:
        servletContext - ServletContext
        contextInitializerClasses - the classes of ApplicationContextInitializer
        See Also:
        ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM
      • setFrameworkServletContextInitializerClassInitParameter

        public static void setFrameworkServletContextInitializerClassInitParameter​(javax.servlet.ServletContext servletContext,
                                                                                   java.lang.Class<? extends org.springframework.context.ApplicationContextInitializer>... contextInitializerClasses)
        Sets initialized parameter for Context Initializer Class into FrameworkServlet
        Parameters:
        servletContext - ServletContext
        contextInitializerClasses - the classes of ApplicationContextInitializer
        See Also:
        FrameworkServlet.applyInitializers(ConfigurableApplicationContext)
      • isPageRenderRequest

        public static boolean isPageRenderRequest​(org.springframework.web.servlet.ModelAndView modelAndView)
        Is page render request
        Parameters:
        modelAndView - ModelAndView
        Returns:
        If current request is for page render , return true , or false.
      • getClassNames

        protected static java.lang.String[] getClassNames​(java.lang.Class<?>... classes)