2013-05-27 21 views
0

我有一個奇怪的問題與Spring MVC。 我有一個控制器方法,接受2日期參數作爲請求參數startDateendDate。 如果我用一個簡單的URL與2個PARAMS像這樣:彈簧mvc錯誤綁定日期參數

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

任何有識之士?

在此先感謝

+3

我認爲'url extension'(如果有的話)應該是路徑的一部分,因此屬於查詢之前:'http://localhost/myapp/videos.json?startDate = 2013-05-10&endDate = 2013 -06-01'。 – nutlike

+0

哦,上帝,我很愚蠢,當然是url的擴展id部分。我得到它與路徑變量感到困惑感謝您指出它.. – ufasoli

+0

我發表了我的評論作爲答案,所以你可以標記你的問題解決。 – nutlike

回答

2

我認爲,「URL擴展」(如果有的話)應該是路徑的一部分,查詢前,因此屬於:http://localhost/myapp/videos.json?startDate=2013-05-10&endDate=2013-06-01