Spring Boot Controller - Enum parameter as Value -
i have spring boot controller, 1 of parameters enum. enum has string value. want pass parameter value of enum , controller give me enum. can done?
@requestmapping(value = "/") public myresponse getresponse ( @requestparam(value = "version") final projectversion version ) { ...bla bla bla... } public enum projectversion { version_1 ("1.00") version_2 ("2.00") private final string version; projectversion ( string version ) { this.version = version; } @override public string tostring() { return this.version; } }
i want able make request follows:
http://myhost.com/mypath?version=1.00
and in controller projectversion.version_1
any ideas?
this not possible is. has create custom converter convert string
projectversion
.
for example first defines converter string
projectversion
:
public class projectversionconverter implements conditionalgenericconverter { @override public boolean matches(typedescriptor sourcetype, typedescriptor targettype) { return targettype.gettype().equals(projectversion.class); } @override public set<convertiblepair> getconvertibletypes() { return collections.singleton(new convertiblepair(string.class , projectversion.class)); } @override public object convert(object source, typedescriptor sourcetype, typedescriptor targettype) { return projectversion.findbyversion((string)source); } }
then register it:
@configuration public class customwebmvcconfigureradapter extends webmvcconfigureradapter { @override public void addformatters(formatterregistry registry) { registry.addconverter(new projectversionconverter()); } }
you can skip registration if define projectversionconverter
has spring bean. (this code has not tested).
Comments
Post a Comment