关于SpringMVC在Controller层方法的参数解析详解
使用版本:
spring-boot: 2.1.6.release
sping: 5.1.8.release
java: openjdk 11.0.13
自定义参数解析器
为了能够实现自定义参数解析器,只需要实现 handlermethodargumentresolver 并将添加到 webmvcconfigurer#addargumentresolvers 即可。
实现效果
下面以添加一个获取请求头的 client-type 参数为例进行是实现。定义注解 @clienttypemark 以及enum clienttype ,用于注入header中的值。
@target({elementtype.parameter}) @retention(retentionpolicy.runtime) @documented public @interface clienttypemark { }
public enum clienttype { web, mobile, unknown ; }
最终实现的效果为在controller的方法中,可以按照如下实现使用。
@restcontroller @requestmapping("/client") public class clienttypeendpoint { @getmapping("/cur") public clienttype getcurclienttype(@clienttypemark clienttype clienttype) { return clienttype; } }
实现和配置
按照如下方法实现 handlermethodargumentresolver ,用于解析header中的 client-type 并转化为 clienttype 。
package io.github.donespeak.springbootsamples.web.common; import org.apache.commons.lang3.stringutils; import org.springframework.core.methodparameter; import org.springframework.web.bind.support.webdatabinderfactory; import org.springframework.web.context.request.nativewebrequest; import org.springframework.web.method.support.handlermethodargumentresolver; import org.springframework.web.method.support.modelandviewcontainer; public class curclienttypemethodargumentresolver implements handlermethodargumentresolver { private final string[] client_type_header_names = {"client-type", "client-type", "client-type"}; // 选择要被注入的参数:判断参数是否符合解析的条件 @override public boolean supportsparameter(methodparameter param) { return param.hasparameterannotation(clienttypemark.class) && clienttype.class.isassignablefrom(param.getparametertype()); } // 返回值将被注入到被选中参数中 @override public object resolveargument(methodparameter methodparameter, modelandviewcontainer modelandviewcontainer, nativewebrequest nativewebrequest, webdatabinderfactory webdatabinderfactory) throws exception { string clienttype = null; for (string clienttypeheader: client_type_header_names) { clienttype = nativewebrequest.getheader(clienttypeheader); if (stringutils.isnotblank(clienttype)) { break; } } try { return stringutils.isblank(clienttype) ? clienttype.unknown : clienttype.valueof(clienttype.touppercase()); } catch (exception e) { return clienttype.unknown; } } }
为了使得配置能够生效,可以按照如下的方法添加解析器。
@configuration public class webmvcconfig implements webmvcconfigurer { @override public void addargumentresolvers(list<handlermethodargumentresolver> argumentresolvers) { // 解析@curuserid注解的参数 argumentresolvers.add(new curclienttypemethodargumentresolver()); } }
至此,自定义参数解析器的工作就完成了。
spring提供解析器
当你完成上面的自定义解析器的时候,大体就能知道spring在controller层的其他参数解析都是怎么实现的了。
如下是spring mvc默认的一些参数解析器。
package org.springframework.web.servlet.mvc.method.annotation; public class requestmappinghandleradapter extends abstracthandlermethodadapter implements beanfactoryaware, initializingbean { ... /** * return the list of argument resolvers to use including built-in resolvers * and custom resolvers provided via {@link #setcustomargumentresolvers}. */ private list<handlermethodargumentresolver> getdefaultargumentresolvers() { list<handlermethodargumentresolver> resolvers = new arraylist<>(); // annotation-based argument resolution // 解析:@requestparam(required = false) resolvers.add(new requestparammethodargumentresolver(getbeanfactory(), false)); // 解析:@requestparam map resolvers.add(new requestparammapmethodargumentresolver()); // 解析:@pathvariable resolvers.add(new pathvariablemethodargumentresolver()); // 解析:@pathvariable map resolvers.add(new pathvariablemapmethodargumentresolver()); // 解析:@matrixvariable resolvers.add(new matrixvariablemethodargumentresolver()); // 解析:@matrixvariable map resolvers.add(new matrixvariablemapmethodargumentresolver()); // 解析:@modelattribute(required = false) resolvers.add(new servletmodelattributemethodprocessor(false)); // 解析:@requestbody resolvers.add(new requestresponsebodymethodprocessor(getmessageconverters(), this.requestresponsebodyadvice)); // 解析:@requestpart resolvers.add(new requestpartmethodargumentresolver(getmessageconverters(), this.requestresponsebodyadvice)); // 解析:@requestheader resolvers.add(new requestheadermethodargumentresolver(getbeanfactory())); // 解析:@requestheader map resolvers.add(new requestheadermapmethodargumentresolver()); // 解析:@cookievalue resolvers.add(new servletcookievaluemethodargumentresolver(getbeanfactory())); // 解析:@value resolvers.add(new expressionvaluemethodargumentresolver(getbeanfactory())); // 解析:@sessionattribute resolvers.add(new sessionattributemethodargumentresolver()); // 解析:@requestattribute resolvers.add(new requestattributemethodargumentresolver()); // type-based argument resolution // 解析:webrequest、servletrequest、multipartrequest、httpsession、 // principal、locale、timezone、java.time.zoneid、inputstream // java.io.reader、org.springframework.http.httpmethod resolvers.add(new servletrequestmethodargumentresolver()); // 解析:servletresponse、outputstream、writer resolvers.add(new servletresponsemethodargumentresolver()); // 解析:@httpentity、@requestentity resolvers.add(new httpentitymethodprocessor(getmessageconverters(), this.requestresponsebodyadvice)); // 解析:redirectattributes resolvers.add(new redirectattributesmethodargumentresolver()); // 解析:org.springframework.ui.model, 值为modelandviewcontainer#getmodel resolvers.add(new modelmethodprocessor()); // 解析:map, 值为modelandviewcontainer#getmodel resolvers.add(new mapmethodprocessor()); // 解析:org.springframework.validation.errors resolvers.add(new errorsmethodargumentresolver()); // 解析:org.springframework.web.bind.support.sessionstatus resolvers.add(new sessionstatusmethodargumentresolver()); // 解析:uricomponentsbuilder 或者 servleturicomponentsbuilder resolvers.add(new uricomponentsbuildermethodargumentresolver()); // custom arguments if (getcustomargumentresolvers() != null) { resolvers.addall(getcustomargumentresolvers()); } // catch-all // 解析:@requestparam(required = true) resolvers.add(new requestparammethodargumentresolver(getbeanfactory(), true)); // 解析:@modelattribute(required = true) resolvers.add(new servletmodelattributemethodprocessor(true)); return resolvers; } ... }
一些使用tricky
调整解析器的顺序
在一次使用spring security的开发中,定义的 userdetials 实现类在实现 userdetails 的同时还是实现了一个 account 接口,在使用时,希望实现如下的效果。
public void dosomething(@authenticationprincipal account accout) {}
但因为同时使用了,spring-data,spring-data提供的 proxyinghandlermethodargumentresolver 会在 authenticationprincipalargumentresolver 前面对account进行注入处理。proxyinghandlermethodargumentresolver 会拦截掉所有非org.springframework中定义的 接口 ,并尝试赋值。为了能够正常赋值给account,需要调整两者的位置。实现如下:
import java.util.linkedlist; import java.util.list; import org.springframework.beans.beansexception; import org.springframework.beans.factory.config.beanpostprocessor; import org.springframework.security.web.method.annotation.authenticationprincipalargumentresolver; import org.springframework.stereotype.component; import org.springframework.web.method.support.handlermethodargumentresolver; import org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter; /** * 将 authenticationprincipalargumentresolver 移到最前面,如果不做调整, * 会因为proxyinghandlermethodargumentresolver在authenticationprincipalargumentresolver前面, * 导致 @authenticationprincipal account account 无法注入 */ public class argumentresolverbeanpostprocessor implements beanpostprocessor { private static final string bean_request_mapping_handler_adapter = "requestmappinghandleradapter"; @override public object postprocessafterinitialization(object bean, string beanname) throws beansexception { if (beanname.equals(bean_request_mapping_handler_adapter)) { requestmappinghandleradapter adapter = (requestmappinghandleradapter)bean; list<handlermethodargumentresolver> argumentresolvers = adapter.getargumentresolvers(); linkedlist<handlermethodargumentresolver> resolversadjusted = new linkedlist<>(argumentresolvers); argumentresolvers.stream().foreach(r -> { if (authenticationprincipalargumentresolver.class.isinstance(r)) { resolversadjusted.addfirst(r); } else { resolversadjusted.add(r); } }); adapter.setargumentresolvers(resolversadjusted); } return bean; } }
总结
到此这篇关于springmvc在controller层方法的参数解析的文章就介绍到这了,更多相关springmvc在controller层方法参数内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: Java KeyGenerator.generateKey的19个方法代码示例
下一篇: Day03