【发布时间】:2016-12-12 08:57:02
【问题描述】:
在我的带有 @PathVariable("timestamp) 的 REST API 控制器中,我必须验证时间戳格式是否符合 ISO 8601 标准:例如 2016-12-02T18:25:43.511Z。
@RequestMapping("/v3/testMe/{timestamp}")
public class TestController {
private static final String HARDCODED_TEST_VALUE = "{\n\t\"X\": \"01\",\n\t\"Y\": \"0.2\"\n}";
@ApiOperation(nickname = "getTestMe", value = "Return TestMe value", httpMethod = "GET",
authorizations = {@Authorization(value = OAUTH2,
scopes = {@AuthorizationScope(scope = DEFAULT_SCOPE, description = SCOPE_DESCRIPTION)})})
@RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String getTestMe(@PathVariable("timestamp") String timestamp) {
if (timestamp != null) {
return HARDCODED_TEST_VALUE;
}
throw new ResourceNotFoundException("wrong timestamp format");
}
}
我想要实现它的方式类似于上面检查时间戳是否为空的 if-else 语句 - 所以类似地,我想添加类似的 if-else 来验证时间戳的格式并返回正文 if so 或 404 错误代码,如果不是。
知道我可以用什么来做到这一点,请给我准备好的例子吗?我尝试过使用正则表达式进行简单验证,但不方便,不幸的是无论如何都没有用......
【问题讨论】:
-
" 404 错误代码,如果不是" - 这不是该错误的正确代码。在这里得到 404,我作为客户端会检查(当然没有结果)服务器是否可用以及我是否使用了正确的 URI ......只是说。
标签: java rest api validation timestamp