Springboot (b) handlermethodargumentresolver

Source: Internet
Author: User

As follows: During the development process, you may use Requestparam annotations, set required = True, tell the front-end that this parameter is mandatory,
But the basic understanding used, usually because the front-end passed an empty string, resulting in a validation pass, poor practicality,
Native annotations are basically not enough to meet actual development needs, so you need to write one yourself.

@ResponseBody @requestmapping ("/data")  Public map<string, object> data (        true) String b) {    returnnew Hashmap<>();}
Handlermethodargumentresolver

At this point you need to write a section, Handlermethodargumentresolver, literal translation is, the decomposition of the function parameters, the implementation of this interface is also very simple,

Supportsparameter (Support parameters): You can set some flags, indicating that you can handle these parameters, return ture to execute the resolveargument () function
Resolveargument (Decomposition arguments): The specific method of handling the arguments

ImportOrg.springframework.core.MethodParameter;Importorg.springframework.web.bind.support.WebDataBinderFactory;Importorg.springframework.web.context.request.NativeWebRequest;ImportOrg.springframework.web.method.support.HandlerMethodArgumentResolver;ImportOrg.springframework.web.method.support.ModelAndViewContainer;/*** Created by 12614 on 2018/5/11.*/ Public classTestargumentresolverImplementsHandlermethodargumentresolver {@Override Public BooleanSupportsparameter (Methodparameter methodparameter) {return true; } @Override PublicObject resolveargument (methodparameter methodparameter, Modelandviewcontainer Modela Ndviewcontainer, Nativewebrequest nativewebrequest, WebD Atabinderfactory webdatabinderfactory)throwsException {System.out.println ("Testargumentresolver:resolveargument"); return NULL; }}
Spring Configuration
< Mvc:annotation-driven >      < mvc:argument-resolvers >        <  class= "xxxxx Full class name"/>      </MVC: Argument-resolvers></mvc:annotation-driven>
Springboot Configuration

Since the 0 configuration, do not violate the original design deliberately to enable the XML configuration, add Handlermethodargumentresolver in the Webmvcconfigureradapter is good ,

Importorg.springframework.context.annotation.Configuration;ImportOrg.springframework.web.method.support.HandlerMethodArgumentResolver;ImportOrg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;Importjava.util.List;/*** Created by 12614 on 2018/5/11.*/@Configuration Public classApplicationconfigurerextendsWebmvcconfigureradapter {@Override Public voidAddargumentresolvers (listargumentresolvers) {        Super. Addargumentresolvers (argumentresolvers); Argumentresolvers.add (Newtestargumentresolver ()); }}
Simple Case-Custom annotations
Importjava.lang.annotation.*;/*** Created by 12614 on 2018/5/11.*/@Documented @target (elementtype.parameter) @Retention (retentionpolicy.runtime) Public@InterfaceParams {@AliasFor ("Name") String value ()default""; @AliasFor ("Value") String name ()default""; BooleanRequired ()default true; BooleanNotempty ()default true; intMaxLength ()defaultInteger.max_value; String DefaultValue ()default"\n\t\t\n\t\t\n\ue000\ue001\ue002\n\t\t\t\t\n";}
Parametric decomposition device
ImportOrg.springframework.core.MethodParameter;Importorg.springframework.web.bind.support.WebDataBinderFactory;Importorg.springframework.web.context.request.NativeWebRequest;ImportOrg.springframework.web.method.support.HandlerMethodArgumentResolver;ImportOrg.springframework.web.method.support.ModelAndViewContainer;Importjavax.servlet.http.HttpServletRequest;/*** Created by 12614 on 2018/5/11.*/ Public classTestargumentresolverImplementsHandlermethodargumentresolver {@Override Public BooleanSupportsparameter (Methodparameter methodparameter) {//If the function contains our custom annotations, go to the resolveargument () function        returnMethodparameter.hasparameterannotation (Params.class); } @Override PublicObject resolveargument (methodparameter methodparameter, Modelandviewcontainer Modela Ndviewcontainer, Nativewebrequest nativewebrequest, WebD Atabinderfactory webdatabinderfactory)throwsException {System.out.println ("Testargumentresolver:resolveargument"); params params= Methodparameter.getparameterannotation (Params.class); String paramname=Params.name (); if(ParamName = =NULL) {paramname=Methodparameter.getparametername (); }        //Simple case: If the client does not pass the value, set the default valueObject res = nativewebrequest.getnativerequest (httpservletrequest.class). GetParameter (paramname); returnres = =NULL?params.defaultvalue (): res; }}
Usage Scenarios
    @ResponseBody    @RequestMapping ("/data")    public map<string, object> data (            true, maxLength = DefaultValue = "AAA") String a) {        System.out.println (a);         returnnull;    }

Springboot (b) handlermethodargumentresolver

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.