Interface parameter resolution - fixed type resolution

In the actual project, we often get the following interfaces: HttpServletRequest, HttpServletResponse. We will find that...
Processor processing description
Processor processing description
Processor processing description
Processor processing description
Processor processing description
Processor processing description
Processor processing description

In the actual project, we often get the following interfaces: HttpServletRequest, HttpServletResponse. We will find that we can get the object without any processing. This is the charm of fixed type parsers. In addition to the above two parameters, such as sessionstatus, servletresponse, OutputStream, writer, webrequest, multipartrequest, httpsession, prin API objects generated from cipal and InputStream can also be obtained

1, ServletRequestMethodArgumentResolver
@Override public boolean supportsParameter(MethodParameter parameter) { Class<?> paramType = parameter.getParameterType(); return (WebRequest.class.isAssignableFrom(paramType) || ServletRequest.class.isAssignableFrom(paramType) || MultipartRequest.class.isAssignableFrom(paramType) || HttpSession.class.isAssignableFrom(paramType) || (pushBuilder != null && pushBuilder.isAssignableFrom(paramType)) || Principal.class.isAssignableFrom(paramType) || InputStream.class.isAssignableFrom(paramType) || Reader.class.isAssignableFrom(paramType) || HttpMethod.class == paramType || Locale.class == paramType || TimeZone.class == paramType || ZoneId.class == paramType); } @Override public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception { Class<?> paramType = parameter.getParameterType(); // WebRequest / NativeWebRequest / ServletWebRequest if (WebRequest.class.isAssignableFrom(paramType)) { if (!paramType.isInstance(webRequest)) { throw new IllegalStateException( "Current request is not of type [" + paramType.getName() + "]: " + webRequest); } return webRequest; } // ServletRequest / HttpServletRequest / MultipartRequest / MultipartHttpServletRequest if (ServletRequest.class.isAssignableFrom(paramType) || MultipartRequest.class.isAssignableFrom(paramType)) { return resolveNativeRequest(webRequest, paramType); } // HttpServletRequest required for all further argument types return resolveArgument(paramType, resolveNativeRequest(webRequest, HttpServletRequest.class)); }

Processor processing description

  • If the parameter type is WebRequest, ServletRequest, MultipartRequest, HttpSession, pushBuilder,Principal,InputStream,Reader,HttpMethod,Locale,TimeZone,ZoneId
    You will use this processor. You should understand that if you need to use these parameters in the future, you can declare them directly on the method, and you don't need to get them by yourself
2, ServletResponseMethodArgumentResolver
// @since 3.1 public class ServletResponseMethodArgumentResolver implements HandlerMethodArgumentResolver { // It's relatively simple @Override public boolean supportsParameter(MethodParameter parameter) { Class<?> paramType = parameter.getParameterType(); return (ServletResponse.class.isAssignableFrom(paramType) || // webRequest.getNativeResponse(requiredType) OutputStream.class.isAssignableFrom(paramType) || //response.getOutputStream() Writer.class.isAssignableFrom(paramType)); //response.getWriter() } @Override public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception { // That's the judgment... if (mavContainer != null) { mavContainer.setRequestHandled(true); } ... } }

Processor processing description

  • If the parameter type is ServletResponse, OutputStream, the Writer will use this processor
3, SessionStatusMethodArgumentResolver
public class SessionStatusMethodArgumentResolver implements HandlerMethodArgumentResolver { @Override public boolean supportsParameter(MethodParameter parameter) { return SessionStatus.class == parameter.getParameterType(); } @Override public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception { Assert.state(mavContainer != null, "ModelAndViewContainer is required for session status exposure"); return mavContainer.getSessionStatus(); } }

Processor processing description

  • This handler is used if the parameter type is SessionStatus
4, UriComponentsBuilderMethodArgumentResolver
public class UriComponentsBuilderMethodArgumentResolver implements HandlerMethodArgumentResolver { @Override public boolean supportsParameter(MethodParameter parameter) { Class<?> type = parameter.getParameterType(); return (UriComponentsBuilder.class == type || ServletUriComponentsBuilder.class == type); } @Override public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception { HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class); Assert.state(request != null, "No HttpServletRequest"); return ServletUriComponentsBuilder.fromServletMapping(request); } }

Processor processing description

  • If the parameter type is UriComponentsBuilder, ServletUriComponentsBuilder will use this processor

It is very convenient to get all parts of the URL and build the URL through UriComponentsBuilder.

5, RedirectAttributesMethodArgumentResolver
public class RedirectAttributesMethodArgumentResolver implements HandlerMethodArgumentResolver { @Override public boolean supportsParameter(MethodParameter parameter) { return RedirectAttributes.class.isAssignableFrom(parameter.getParameterType()); } @Override public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception { Assert.state(mavContainer != null, "RedirectAttributes argument only supported on regular handler methods"); ModelMap redirectAttributes; if (binderFactory != null) { DataBinder dataBinder = binderFactory.createBinder(webRequest, null, DataBinder.DEFAULT_OBJECT_NAME); redirectAttributes = new RedirectAttributesModelMap(dataBinder); } else { redirectAttributes = new RedirectAttributesModelMap(); } mavContainer.setRedirectModel(redirectAttributes); return redirectAttributes; } }

Processor processing description

  • This handler is used if the parameter type is RedirectAttributes

RedirectAttributes is a function after spring MVC version 3.1. It is specially used for redirecting and jumping with parameters
If redirection is involved: multiple views can see the values passed, it is more convenient to use it.

6, ModelMethodProcessor
public class ModelMethodProcessor implements HandlerMethodArgumentResolver, HandlerMethodReturnValueHandler { @Override public boolean supportsParameter(MethodParameter parameter) { return Model.class.isAssignableFrom(parameter.getParameterType()); } @Override @Nullable public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception { Assert.state(mavContainer != null, "ModelAndViewContainer is required for model exposure"); return mavContainer.getModel();

Processor processing description

  • This handler is used if the parameter type is Model
7, ModelAttributeMethodProcessor
public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResolver, HandlerMethodReturnValueHandler { private final boolean annotationNotRequired; @Override public boolean supportsParameter(MethodParameter parameter) { return (parameter.hasParameterAnnotation(ModelAttribute.class) || (this.annotationNotRequired && !BeanUtils.isSimpleProperty(parameter.getParameterType()))); } @Override @Nullable public final Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception { Assert.state(mavContainer != null, "ModelAttributeMethodProcessor requires ModelAndViewContainer"); Assert.state(binderFactory != null, "ModelAttributeMethodProcessor requires WebDataBinderFactory"); String name = ModelFactory.getNameForParameter(parameter); ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class); if (ann != null) { mavContainer.setBinding(name, ann.binding()); } Object attribute = null; BindingResult bindingResult = null; if (mavContainer.containsAttribute(name)) { attribute = mavContainer.getModel().get(name); } else { // Create attribute instance try { attribute = createAttribute(name, parameter, binderFactory, webRequest); } catch (BindException ex) { if (isBindExceptionRequired(parameter)) { // No BindingResult parameter -> fail with BindException throw ex; } // Otherwise, expose null/empty value and associated BindingResult if (parameter.getParameterType() == Optional.class) { attribute = Optional.empty(); } bindingResult = ex.getBindingResult(); } } if (bindingResult == null) { // Bean property binding and validation; // skipped in case of binding failure on construction. WebDataBinder binder = binderFactory.createBinder(webRequest, attribute, name); if (binder.getTarget() != null) { if (!mavContainer.isBindingDisabled(name)) { bindRequestParameters(binder, webRequest); } validateIfApplicable(binder, parameter); if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) { throw new BindException(binder.getBindingResult()); } } // Value type adaptation, also covering java.util.Optional if (!parameter.getParameterType().isInstance(attribute)) { attribute = binder.convertIfNecessary(binder.getTarget(), parameter.getParameterType(), parameter); } bindingResult = binder.getBindingResult(); } // Add resolved attribute and BindingResult at the end of the model Map<String, Object> bindingResultModel = bindingResult.getModel(); mavContainer.removeAttributes(bindingResultModel); mavContainer.addAllAttributes(bindingResultModel); return attribute; }

Processor processing description

  • This handler is used if the parameter type uses @ ModelAttribute or is not a normal type (judged by! BeanUtils.isSimpleProperty)
qq_41071876 Published 2 original articles, praised 0, visited 22 Private letter follow

7 February 2020, 12:27 | Views: 8473

Add new comment

For adding a comment, please log in
or create account

0 comments