java 如何在 Spring 中注册自定义类型转换器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/67980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 10:56:00  来源:igfitidea点击:

How do I register a custom type converter in Spring?

javaspring

提问by alexei.vidmich

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?

我需要通过 http 请求参数传递一个 UUID 实例。Spring 需要注册一个自定义类型转换器(来自 String)。我如何注册一个?

回答by MetroidFan2002

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

请在此处查看弹簧参考手册的第 5 章:5.4.2.1。注册额外的自定义 PropertyEditor

回答by alexei.vidmich

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:

我有一个带有 RequestMapping 注释的 MVC 控制器。一种方法具有 UUID 类型的参数。感谢工具包,在阅读了WebDataBinder 之后,我想我的控制器中需要一个这样的方法:

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.registerCustomEditor(UUID.class, new UUIDEditor());
}

UUIDEditor simply extends PropertyEditorSupport and overrides getAsText() and setAsText().

UUIDEditor 只是扩展了 PropertyEditorSupport 并覆盖了 getAsText() 和 setAsText()。

Worked for me nicely.

很好地对我来说有效。

回答by David Newcomb

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

}

回答by toolkit

Not sure what you are asking?

不确定你在问什么?

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

Spring 带有一个CustomEditorConfigurer来提供自定义 String <-> Object 转换器。

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.

要使用它,只需将 CustomEditorConfigurer 作为 bean 添加到您的配置中,并添加自定义转换器。但是,这些转换器通常用于将配置文件中的字符串属性转换为真实对象。

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

如果您使用的是 Spring MVC,请查看有关带注释的 MVC的部分

Specifically, have a look at the @RequestParamand the @ModelAttributeannotations?

具体来说,看看@RequestParam@ModelAttribute注释?

Hope this helps?

希望这可以帮助?