Java 如何在@RequestMapping 参数中检查请求参数是否为空字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32475877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:37:25  来源:igfitidea点击:

How to check request parameter is not empty String in @RequestMapping params?

javaspringspring-mvchibernate-validatorspring-annotations

提问by WeGa

In my controller I have String parameter, containing some id, that should not be null of empty string. I'm wondering, is there any way to check it is not empty String in @RequestMapping params? I have tried to solve it in some ways

在我的控制器中,我有 String 参数,包含一些 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)

with no success. As I understand, both params = {"id"}and @RequestParam(required = true)can only check that parameter idis presented in request (!= null).

没有成功。据我了解,双方params = {"id"}@RequestParam(required = true)只能检查参数id呈现在请求(!= NULL)。

It is most likely that I have to check that with code in controller boby, like

我很可能必须使用控制器 boby 中的代码进行检查,例如

if (id == null || id.isEmpty()) {
    return someErrorResponse;
}

but please correct me if I wrong. Thanks in advance.

但如果我错了,请纠正我。提前致谢。

P.S. my app is running on Java 1.7 SE in Apache Tomcat 7.0.62 container

PS 我的应用程序在 Apache Tomcat 7.0.62 容器中的 Java 1.7 SE 上运行

采纳答案by Sergio

According to the Spring code that consumes that annotation

根据使用该注释的 Spring 代码

org.springframework.web.servlet.mvc.annotation.ServletAnnotationMappingUtils.checkParameters(String[], HttpServletRequest)

Something like this should work:

这样的事情应该工作:

@RequestMapping(value = someURL, params = {"id!="})
public SomeResponse doSomething(@RequestParam(required = true) String id)

回答by luboskrnac

If you want to be Java EE compliant with validations use @Size(min=1).

如果您希望 Java EE 与验证兼容,请使用@Size(min=1).

Hibernate Validator has @NotEmptyannotation for this purpose. But that's not part of Java spec.

Hibernate Validator 具有@NotEmpty用于此目的的注释。但这不是 Java 规范的一部分。

BTW, keep required=trueas above notifications wouldn't enforce presence of the param in request.

顺便说一句,保持required=true上述通知不会强制请求中存在参数。

EDIT reaction on comment:

编辑对评论的反应:

Spring is Java EE Validation compliant, so @Size annotation should work if you have some Java EE validation API implmentor on class path. I used only hibernate validator so far. So you can enable these validation features by adding this into classpath:

Spring 与 Java EE 验证兼容,因此如果您在类路径上有一些 Java EE 验证 API 实现器,@Size 注释应该可以工作。到目前为止,我只使用了休眠验证器。因此,您可以通过将其添加到类路径中来启用这些验证功能:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.2.1.Final</version>
</dependency>

than you can annotate your controller with @Validatedannotation and do this:

比您可以用@Validated注释来注释您的控制器并执行以下操作:

@RequestMapping(value = someURL, params = {"id"})
public SomeResponse doSomething(@RequestParam(required = true) @Size(min=1) String id)

You can also customize error message that will be sent to client.

您还可以自定义将发送给客户端的错误消息。

If you are not familiar with Java EE validation, I would suggest to start looking into it as it's super important if you are creating REST/HTTP endpoint. It will open new world for you.

如果您不熟悉 Java EE 验证,我建议您开始研究它,因为如果您正在创建 REST/HTTP 端点,它非常重要。它将为您打开新世界。

Start with Java EE Tutorial for validation

从 Java EE 教程开始进行验证

Relevant part of Spring doc

Spring doc的相关部分

回答by Obi Wan - PallavJha

You can use defaultValueas in

您可以使用defaultValue

@RequestParam(value = "id", required = true, defaultValue = "-1") String id

But, again you'll have to use an if like

但是,你必须再次使用 if like

if(id.equals("-1")){
   return "someErrorPage"
}

Otherwise you can create a filter for this purpose, as given in this example

否则,您可以为此目的创建一个过滤器,如本例所示

How to use a servlet filter in Java to change an incoming servlet request url?

如何在 Java 中使用 servlet 过滤器来更改传入的 servlet 请求 url?