在我的控制器中,我有一个字符串参数,包含一些id,它不应该为null或空字符串。我想知道,在@RequestMapping参数中,是否有任何方法可以检查它不是空字符串?我已经尝试了一些解决方法。
@RequestMapping(value = someURL, params = {"id"})
public SomeResponse doSomething(@RequestParam(required = true) String id)
@RequestMapping(value = someURL, params = {"!id="})
public SomeResponse doSomething(@RequestParam(required = true) String id)
@RequestMapping(value = someURL, params = {"!id=\"\""})
public SomeResponse doSomething(@RequestParam(required = true) String id)
没有取得成功。
据我理解,params = {"id"}
和@RequestParam(required = true)
两者都只能检查请求中是否包含参数id
(!= null)。
最可能的情况是我需要在控制器代码中检查它,比如:
if (id == null || id.isEmpty()) {
return someErrorResponse;
}
但是如果我错了,请纠正我。提前致谢。
附言:我的应用运行在Java 1.7 SE和Apache Tomcat 7.0.62容器上。
{"status":400,"error":"Bad Request","exception":"org.springframework.web.bind.ServletRequestBindingException","message":"Missing request header 'X-Custom-Header' for method parameter of type String","path":"/api/"}
。 - user3105453