java - Spring aliasFor for Annotations with Target(PARAMETER) -
i trying use meta-annotation of spring using aliasfor annotation create custom annotation springs requestparam
simply 'extend/replace'
@target(elementtype.parameter) @retention(retentionpolicy.runtime) @documented public @interface requestparam { @aliasfor("name") string value() default ""; ---- }
with annotation
@target(elementtype.parameter) @retention(retentionpolicy.runtime) @inherited public @interface queryparam { @aliasfor(annotation = requestparam.class, attribute = "name") string name() default ""; @aliasfor(annotation = requestparam.class, attribute = "required") boolean required() default false; @aliasfor(annotation = requestparam.class, attribute = "defaultvalue") string defaultvalue() default valueconstants.default_none; }
this way throws exception
org.springframework.core.annotation.annotationconfigurationexception: @aliasfor declaration on attribute [name] in annotation [package.queryparam] declares alias attribute [name] in meta-annotation [org.springframework.web.bind.annotation.requestparam] not meta-present.
problem without requestparam annotated on queryparam doesn't work. , not possible put requestparam parameter targeted.
@requestparam <--this not possible. public @interface queryparam
so there way achieve ?
basically want achieve not possible now, @ least spring v 4.3.3 there main 2 problems, first 1 fact annotations @requestparam
declared @target(elementtype.parameter)
make impossible used part of meta annotations. furthermore, spring mvc looks annotations on method parameters using org.springframework.core.methodparameter.getparameterannotations()
not support meta-annotations or composed annotations. if need customizations there can use handlermethodargumentresolver
instead of meta annotations.
so code like
@target(elementtype.parameter) @retention(retentionpolicy.runtime) @documented public @interface queryparam { string name() default ""; boolean required() default false; string defaultvalue() default valueconstants.default_none; }
then using handlermethodargumentresolver
add custom logic need.
public class queryparamresolver implements handlermethodargumentresolver { public boolean supportsparameter(methodparameter parameter) { return parameter.getparameterannotation(queryparam.class) != null; } public object resolveargument(methodparameter parameter, modelandviewcontainer mavcontainer, nativewebrequest webrequest, webdatabinderfactory binderfactory) throws exception { queryparam attr = parameter.getparameterannotation(queryparam.class); // here can use logic need return webrequest.getparameter(attr.value()); } }
then need register our handlermethodargumentresolver
@configuration @enablewebmvc public class config extends webmvcconfigureradapter { @override public void addargumentresolvers(list<handlermethodargumentresolver> argumentresolvers) { argumentresolvers.add(new queryparamresolver()); } }
and last lets use our custom annotation
@getmapping("/test") public string test(@queryparam("foo") string foo){ // here }
Comments
Post a Comment