5

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?

4 Answers 4

4

Please see chapter 5 of the spring reference manual here: 5.4.2.1. Registering additional custom PropertyEditors

4

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.

2

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)) ;
  }

}
0

Not sure what you are asking?

Spring comes with a CustomEditorConfigurer to supply custom String <-> Object converters.

To use this, just add the CustomEditorConfigurer as bean to your config, and add the custom converters. However, these converters are typically used when converting string attributes in the config file into real objects.

If you are using Spring MVC, then take a look at the section on annotated MVC

Specifically, have a look at the @RequestParam and the @ModelAttribute annotations?

Hope this helps?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.