是否可以在Spring MVC REST端点中验证@RequestParam? [英] Is it possible to validate @RequestParam in Spring MVC REST endpoint?

查看:149
本文介绍了是否可以在Spring MVC REST端点中验证@RequestParam?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在泽西岛2,可以这样做:

In Jersey 2 it is possible to do this:

@GET
@PATH("user/{email}")
public IDto getUser(@NotNull @Email @PathParam("email") String validEmail) {
    return userManagementService.findUserByEmail(validEmail);
}

但是我无法在Spring MVC中做类似工作,似乎验证仅在@RequestBody中提供对象或使用SpringMVC表单时完成,例如以下操作无效:

But I cannot make something similar to work in Spring MVC, it seems that the validation is only done when providing an object in @RequestBody or using an SpringMVC Form, for example the following won't work:

@RequestMapping(value="/user/{email}", method = RequestMethod.GET)
public @ResponseBody IDto getUser(@NotNull @Email @PathVariable String validEmail) {
    return userManagementService.findUserByEmail(validEmail);
}   

还有其他类似的问题,但这些问题似乎都面向Spring MVC UI应用程序,在我的情况下,它只是一个返回JSON响应的REST API,所以我没有任何View映射/绑定到控制器。

There are other similar questions, but those seem to be oriented to Spring MVC UI applications, in my case it is only a REST API which returns JSON response so I don't have any View to map/bind to the controller.

推荐答案

似乎可以,使用 @Validated

这是一个示例

这篇关于是否可以在Spring MVC REST端点中验证@RequestParam?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆