在我的项目中,我使用了Spring + Scala。在Spring MVC中使用Scala类作为DTO
我的一些Spring MVC控制器使用Spring功能将传入HTTP参数绑定到DTO对象。就像这样:
@RequestMapping(value = Array("/", ""), method = Array(RequestMethod.POST))
def saveProduct(dto: MyDto): Iterable[MyDto] = {...}
而且MyDto
是简单的Scala类:
class MyDto extends Serializable {
@BeanProperty var id : Long = _
@BeanProperty var name: String = _
}
我的问题是当试图使用Scala的Option
类字段MyDto
是我得到的例外:
class MyDto extends Serializable {
@BeanProperty var id : Option[Long] = None
@BeanProperty var name: Option[String] = None
}
异常消息是:
Failed to convert property value of type 'java.lang.String' to required type 'scala.Option' for property 'name';
我可以做什么使用Scala选项作为类型如果字段在MyDto
?
它可能会有点复杂,但您需要[注册一个自定义的'PropertyEditor'](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation .html#beans-beans-conversion-customeditor-registration)来处理'Option [T]'类。问题很可能是它正确处理参数化类型'T' :-( – millhouse 2015-03-31 23:22:59