I need to pass a UUID instance via http request parameter. Spring needs a custom type converter (from String) to be registered. How do I register one?
Please see chapter 5 of the spring reference manual here: 5.4.2.1. Registering additional custom PropertyEditors
I have an MVC controller with RequestMapping annotations. One method has a parameter of type UUID. Thanks toolkit, after reading about WebDataBinder, I figured that I need a method like this in my controller:
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(UUID.class, new UUIDEditor());
}
UUIDEditor simply extends PropertyEditorSupport and overrides getAsText() and setAsText().
Worked for me nicely.
In extenstion to the previous example.
Controller class
@Controller
@RequestMapping("/showuuid.html")
public class ShowUUIDController
{
@InitBinder
public void initBinder(WebDataBinder binder)
{
binder.registerCustomEditor(UUID.class, new UUIDEditor());
}
public String showuuidHandler (@RequestParam("id") UUID id, Model model)
{
model.addAttribute ("id", id) ;
return "showuuid" ;
}
}
Property de-munger
class UUIDEditor extends java.beans.PropertyEditorSupport
{
@Override
public String getAsText ()
{
UUID u = (UUID) getValue () ;
return u.toString () ;
}
@Override
public void setAsText (String s)
{
setValue (UUID.fromString (s)) ;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With