有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

为什么通过python调用my api会给出无效的日期格式,而在java中则不会(使用相同的字符串)

我有一个使用spring的Java API:

    @ApiOperation(value = "Retrieve Tasks Results", notes = "Getting Successful Run Results for an Egg, Task, within date range")
@GetMapping(value = "{eggID}/tasks/{taskIDs}/results", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String, List<TaskResult>>> getTasksRunResults(@PathVariable("eggID") String eggID,
                                                             @PathVariable("taskIDs") List<String> taskIDs,
                                                             @RequestParam(value = "fromDate", required = true)
                                                                 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS Z")
                                                                         Date fromDate,
                                                             @RequestParam(value = "toDate", required = true)
                                                                 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS Z")
                                                                         Date toDate) throws MoLiException {

我可以使用以下url调用:

String s = "/eggs/00000000-0333-0001-0002-000000000000/tasks/[00000000-0222-0026-0000-000000000000]/results?fromDate=2010-01-15 18:25:48.104 +0000&toDate=2026-01-15 18:25:48.104 +0000";
return mockMvc.perform(get(s).contentType(MediaType.APPLICATION_JSON_VALUE));

但是,当我尝试通过python代码使用相同的字符串时:

url = '/eggs/00000000-0333-0001-0002-000000000000/tasks/[00000000-0222-0026-0000-000000000000]/results?fromDate=2010-01-15 18:25:48.104 +0000&toDate=2026-01-15 18:25:48.104 +0000'
response = request_handler.send_request(url) 

我得到以下错误:

    Exception: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.util.Date] for value '2010-01-15 18:25:48.104  0000'; 
nested exception is java.lang.IllegalArgumentException: Invalid format: "2010-01-15 18:25:48.104  0000" is malformed at " 0000". Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; 
nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.util.Date] for value '2010-01-15 18:25:48.104  0000'; 
nested exception is java.lang.IllegalArgumentException: Invalid format: "2010-01-15 18:25:48.104  0000" is malformed at " 0000"

有人能解释一下为什么,或者至少给我一些我可以尝试的东西吗

PS.请求处理程序代码为:

    def send_request(self, action, method="GET",  **kwargs):
    request_arguments = {
        "method": method,
        "url": self._build_url(self.url, action),
        "headers": self._get_headers(**kwargs),
        "data": kwargs["data"] if "data" in kwargs else {},
        "verify": False
    }

@staticmethod
def _build_url(*args):
    url = "".join(args)
    return url

共 (0) 个答案