在Spring中绑定变量时处理长类型上的字符串

在Spring中绑定变量时处理长类型上的字符串,spring,spring-mvc,data-binding,numberformatexception,Spring,Spring Mvc,Data Binding,Numberformatexception,在UI中,我有一个字段,用户可以在其中输入数字或“---”符号。但我对控制器的调用失败,当他们放置“---”时出现NumberFormatException异常,因为该字段在我的DTO中定义为Long。一种可能的解决方案是将Long改为String。我很好奇是否有其他方法来处理这类问题 谢谢。假设“-”为零: @RequestMapping( value = "/some/path/--", method = RequestMethod.GET ) public String doSomethi

在UI中,我有一个字段,用户可以在其中输入数字或“---”符号。但我对控制器的调用失败,当他们放置“---”时出现NumberFormatException异常,因为该字段在我的DTO中定义为Long。一种可能的解决方案是将Long改为String。我很好奇是否有其他方法来处理这类问题

谢谢。

假设“-”为零:

@RequestMapping( value = "/some/path/--", method = RequestMethod.GET )
public String doSomething() throws Exception {
    return doSomething( 0 ); // assumming -- is 0
}

@RequestMapping( value = "/some/path/{id}", method = RequestMethod.GET )
public String doSomething( @PathVariable( "id" ) long id ) {
    return "it worked (" + id + ")";
}

“-”代表什么数字?很抱歉没有明确说明,但这个问题是我在UI表单中的许多输入字段之一。所以我使用@RequestAttribute将输入绑定到bean中的字段,而不是使用/some/path/{id}path。我在BindingResult上得到了这些异常。有什么优雅的方法来处理这种情况吗?