Java Spring MVC 绑定 UUID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20366304/
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
Bind UUID in Spring MVC
提问by Kong
What is the easiest way to bind a UUID in Spring MVC, such that this works:
在 Spring MVC 中绑定 UUID 的最简单方法是什么,使其有效:
@RequestMapping("/MyController.myAction.mvc")
@ResponseBody
public String myAction(UUID id, String myParam)...
Using the above I currentely get the following exception:
使用上述我目前得到以下异常:
org.springframework.beans.BeanInstantiationException:
Could not instantiate bean class [java.util.UUID]:
No default constructor found;
nested exception is java.lang.NoSuchMethodException: java.util.UUID.<init>()
There are other questions on SO that skirt around this, but none seem to answer it. I'm using Spring 3.latest (4 EA actually). I'm after the latest, simplest way to achieve this.
还有其他关于 SO 的问题围绕着这个问题,但似乎没有人回答它。我正在使用 Spring 3.latest(实际上是 4 EA)。我正在寻找最新,最简单的方法来实现这一目标。
采纳答案by M. Deinum
UUID
is a class that cannot simply be instantiated. Assuming that it comes as a request parameter you should first annotate the argument with @RequestParam
.
UUID
是一个不能简单实例化的类。假设它是一个请求参数,你应该首先用@RequestParam
.
@RequestMapping("/MyController.myAction.mvc")
@ResponseBody
public String myAction(@RequestParam UUID id, String myParam)...
Now this expects a request parameter with the name id
to be available in the request. The parameter will be converted to a UUID
. However at the moment this will fail because there is, currently, nothing that can convert from a String
to a UUID
.
现在,这需要一个具有名称的请求参数id
在请求中可用。该参数将转换为UUID
. 但是目前这将失败,因为目前没有任何东西可以从 a 转换String
为 a UUID
。
For this create a Converter
which can do this.
为此创建一个Converter
可以做到这一点。
public class StringToUUIDConverter implements Converter<String, UUID> {
public UUID convert(String source) {
return UUID.fromString(source);
}
}
Hook this class up to the ConversionService
and you should have UUID conversion for request parameters. (This would also work if it was a request header, basically for everything that taps into the ConversionService
). You also might want to have a Converter
for the other-way (UUID -> String).
将此类挂接到 上ConversionService
,您应该对请求参数进行 UUID 转换。(如果它是一个请求标头,这也可以工作,基本上适用于进入 的所有内容ConversionService
)。您可能还希望有一个Converter
用于其他方式(UUID -> 字符串)。
Hooking it up to Spring MVC is nicely explained in the reference guide(assuming you use xml config). But in short:
参考指南中很好地解释了将它连接到 Spring MVC (假设您使用 xml 配置)。但简而言之:
<mvc:annotation-driven conversion-service="conversionService"/>
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="org.company.converter.StringToUUIDConverter"/>
</set>
</property>
</bean>
NOTE:As of Spring 3.2 Spring a StringToUUIDConverter
has been added, which automatically registers.
注意:从 Spring 3.2StringToUUIDConverter
开始,添加了Spring a ,它会自动注册。
回答by Abhishek Anand
If its coming as a Header parameter use
如果它作为 Header 参数使用
@RequestHeader(value="UUID") String id
If its coming in a model
如果它有一个模型
@ModelAttribute(value="ModelName") Entity modelName
回答by izilotti
The converter below is available in Spring Framework (core) since version 3.2.
从 3.2 版开始,以下转换器在 Spring Framework(核心)中可用。
org.springframework.core.convert.support.StringToUUIDConverter<String, java.util.UUID>