简体   繁体   English

使用Jersey查询参数验证

[英]query parameter validation with Jersey

I'm using Jersey in Java for a simple web server. 我在Java中使用Jersey来获得一个简单的Web服务器。 I'm trying to handle with url query parameters, like 我正在尝试处理url查询参数,比如

/path?value1=X&value2=Y&value3=Z

I was able to extract the query value by using the @QueryParam annotation like 我能够通过使用@QueryParam注释来提取查询值

@GET
@Produces(MediaType.APPLICATION_XML)
@Path("/path")
public String getUpdate(@QueryParam("All") List<String> allParam, 
        @QueryParam(value = "value1") String value1, 
        @QueryParam(value = "value2") String value2, 
        @QueryParam(value = "value3") String value3) {
        ...
}

Now my problem is I need to validate the input. 现在我的问题是我需要验证输入。 I'm mainly trying to make sure that value1, value2 and value3 follow a specific format. 我主要是为了确保value1,value2和value3遵循特定的格式。 I also want to make sure that those parameters are not empty. 我还想确保这些参数不为空。

I checked out the Bean Validation documentation for Jersey, but it seemed hard to follow. 我查看了Jersey的Bean Validation文档,但似乎很难遵循。 This is probably because I'm still somewhat new to Jersey framework. 这可能是因为我对Jersey框架还有些新意​​。

So how can I set up query parameter validations? 那么如何设置查询参数验证呢? Are there easier to follow resources/examples I could be directed to? 是否更容易关注我可能被引导到的资源/示例? Thanks 谢谢

As @VA31 linked, you can use bean validation like this, if your jersey is latest enough. 当@ VA31链接时,如果您的球衣足够新,您可以像这样使用bean验证。

@GET
@Produces(MediaType.APPLICATION_XML)
@Path("/path")
public Response getUpdate(
    @QueryParam("value1")
    @NotNull
    @Size(min = 1, max = 255)
    @Pattern(regexp = "\\d+")
    String value1, 
    @QueryParam("value2")
    @Min(52)
    @Max(53)
    int value2, 
    @QueryParam("value3")
    @NotNull
    Integer value3) {


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM