简体   繁体   English

RESTful Web服务发布请求验证所需字段

[英]RESTful web service post request validation for required fields

In Java RESTful service request parameter validation, an error should be thrown if the required parameters does not exist in the request payload. 在Java RESTful服务请求参数验证中,如果请求有效负载中不存在必需的参数,则应该引发错误。

I've tried the following but it didn't work: 我已经尝试了以下方法,但是没有用:

public class OrderItemDetailsDTO {

    @XmlElement(required = true)
    private long orderItemId;

    // getters and setters...
}

I also tried @NotNull , @min(1) , but none of them worked. 我也试过@NotNull@min(1)但他们没有工作。 The URL is called and method executes even if the required parameter is not present and then the method throws an exception which I don't want. 即使不存在必需的参数,也会调用URL并执行方法,然后该方法将引发我不希望的异常。

Is there any way so that the error is thrown, saying required element is not present, before going to the method?... 有什么方法可以引发该错误,说该方法之前没有必需的元素?...

Please take a look at this article . 请看一下这篇文章 And here is a small code snippet based on that article as well. 这也是基于该文章的一个小代码段。 I hope this helps 我希望这有帮助

public class OrderItemDetailsDTO {

    @XmlElement
    @Min(1)
    private long orderItemId;

    // getters and setters...
 }

@Path("orders")
public class OrdersResource {
  @POST
  @Consumes({ "application/xml" })
  public void place(@Valid OrderItemDetailsDTO order) {
    // Jersey recognizes the @Valid annotation and
    // returns 400 when the JavaBean is not valid
  }
}

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

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