java - spring 4 enums as controller parameters -


later edit

the case described works properly. problem appears when have on controller's method:

@restcontroller  public class mycontroller {   @requestmapping(...)   public void mymethod(@requestbody myform myform) { ... } }  public class myform {   private x x;   //setters , getters } 

cause

given requestbody, spring use httpmessageconverter deserialize request body instance of given type. in case, use mappingjackson2httpmessageconverter json. converter not involve propertyeditorsupport @ all.

any alternatives? need use @requestbody in case or find way put x myform.


i want put enum parameters inside rest controller's method.

this i've got far.

the enum:

public enum x {   a("a"),b("b"),c("c");   ... methods , constructors ... } 

the controller:

@restcontroller  public class mycontroller {   @requestmapping(...)   public void mymethod(@pathvariable("x") x x) { ... } } 

the configuration:

@controlleradvice public class globalcontrollerconfig {     @initbinder     public void registercustomeditors(webdatabinder binder, webrequest request) {       binder.registercustomeditor(x.class, new xpropertyeditor());     } } 

the property editor:

public class xpropertyeditor extends propertyeditorsupport {     @override     public void setastext(string text) {         try {             setvalue(x.findbyname(text));         } catch (illegalargumentexception e) {             throw new illegalargumentexception("custom binding failed. input type: string. expected type of value set: x", e);         }     }      @override     public string getastext() {         return ((x)getvalue()).getname();     } } 

i put breakpoint in @controlleradvice , passes through binding each time request made of controllers. makes me think binding correct.

when send request method , don't understand why:

org.springframework.http.converter.httpmessagenotreadableexception: not read json: can not construct instance of ...x string value 'a': value not 1 of declared enum instance names: [a, b, c] 

any suggestions?

try change @requestvariable @requestparameter.

i'd change setastext method lines this:

@override public void setastext(string text) {     try {         string uppertext = text.touppercase();          x xresult = x.valueof(uppertext);         setvalue(xresult);     } catch (illegalargumentexception e) {         throw new illegalargumentexception("custom binding failed. input type: string. expected type of value set: x", e);     } } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -