绑定日期参数时弹簧mvc错误

时间:2013-05-27 15:57:32

标签: json spring spring-mvc binding jackson

我对Spring MVC有一个奇怪的问题。 我有一个Controller方法,它接受2个日期参数作为请求参数startDateendDate。 如果我使用带有2个参数的简单网址:

http://localhost/myapp/videos?startDate=2013-05-10&endDate=2013-06-01.json

我收到此错误消息:

[#|2013-05-27T17:39:01.711+0200|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=24;_ThreadName=Thread-2;|38386 [http-thread-pool-8080(5)] DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver  - Resolving exception from handler [public java.util.List<Video> com.ufasoli.Videos.programs(com.ufasoli.filtering.SearchParams)]: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'searchParams' on field 'endDate': rejected value [2013-06-01.json]; codes [typeMismatch.searchParams.endDate,typeMismatch.endDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [searchParams.endDate,endDate]; arguments []; default message [endDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'endDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @org.springframework.format.annotation.DateTimeFormat java.util.Date for value '2013-06-01.json'; nested exception is java.lang.IllegalArgumentException: Invalid format: "2013-06-01.json" is malformed at ".json"]
|#]

但是,只要我删除.json就像这样:

http://localhost/myapp/videos?startDate=2013-05-10&endDate=2013-06-01

一切正常......

这看起来像是一个错误,因为数据绑定器在将数据绑定到控制器时不应该考虑url扩展,还是这种正常行为?

以下是导致问题的控制器方法:

 @RequestMapping(value = "/videos/", 
                 method = RequestMethod.GET, 
                 produces = MediaType.APPLICATION_JSON_VALUE)
    public List<Videos> videos( SearchParams searchParams) {

        return videosRepository.videos(searchParams);
    }

这是我的SearchParams类:

public class SearchParams extends BaseSearchParams implements Serializable{


    private static final long serialVersionUID = 1L;
    @DateTimeFormat(iso = ISO.DATE, pattern = "yyyy-MM-dd")
    private Date startDate;
    @DateTimeFormat(iso = ISO.DATE, pattern = "yyyy-MM-dd")
    private Date endDate;

     //Setters/Getters
}

我正在使用Spring MVC 3.2.1.RELEASE

有什么见解?

提前致谢

1 个答案:

答案 0 :(得分:2)

我认为'url extension'(如果有的话)应该是路径的一部分,因此在查询之前属于http://localhost/myapp/videos.json?startDate=2013-05-10&endDate=2013-06-01