使用 Bean Validation 验证资源数据
Validating Resource Data with Bean Validation
我正在使用 Jersey 2.19 来实现 REST API,但我很难按预期进行资源验证。
我的资源是这样的。
@POST
@Produces(MediaType.TEXT_PLAIN)
@Consumes("application/x-www-form-urlencoded")
public Response test(@NotNull @FormParam("test-param") String testParam, String body)
{
String response = "test-param is: " + testParam + "\n" + "body is: " + body + "\n";
return Response.status(Response.Status.OK).entity(response).build();
}
当我发出以下 cURL 请求时:
curl -X POST http://192.168.50.112:8080/myapp/test-validation --data invalid-param=invalid
我在终端中得到以下响应 window:
test-param is: null
body is: invalid-param=invalid
即test-param
的值如预期的那样是 null
,但不会抛出 Java EE tutorial.
中定义的异常
If a javax.validation.ValidationException or any subclass of
ValidationException except ConstraintValidationException is thrown,
the JAX-RS runtime will respond to the client request with a 500
(Internal Server Error) HTTP status code.
If a ConstraintValidationException is thrown, the JAX-RS runtime will
respond to the client with one of the following HTTP status codes:
500 (Internal Server Error) if the exception was thrown while
validating a method return type
400 (Bad Request) in all other cases
尝试将 public Response test(@FormParam("test-param") @NotNull String testParam, String body)
代替 public Response test(@NotNull @FormParam("test-param") String testParam, String body)
意味着 @NotNull
之后 @FormParam("test-param")
我刚刚偶然发现了答案。 Jersey User Guide 的第 18 章指出
Bean Validation support in Jersey is provided as an extension module
and needs to be mentioned explicitly in your pom.xml file (in case of
using Maven):
我之前只是错过了这个,尽管如果我收到某种警告会有所帮助。
我正在使用 Jersey 2.19 来实现 REST API,但我很难按预期进行资源验证。
我的资源是这样的。
@POST
@Produces(MediaType.TEXT_PLAIN)
@Consumes("application/x-www-form-urlencoded")
public Response test(@NotNull @FormParam("test-param") String testParam, String body)
{
String response = "test-param is: " + testParam + "\n" + "body is: " + body + "\n";
return Response.status(Response.Status.OK).entity(response).build();
}
当我发出以下 cURL 请求时:
curl -X POST http://192.168.50.112:8080/myapp/test-validation --data invalid-param=invalid
我在终端中得到以下响应 window:
test-param is: null
body is: invalid-param=invalid
即test-param
的值如预期的那样是 null
,但不会抛出 Java EE tutorial.
If a javax.validation.ValidationException or any subclass of ValidationException except ConstraintValidationException is thrown, the JAX-RS runtime will respond to the client request with a 500 (Internal Server Error) HTTP status code.
If a ConstraintValidationException is thrown, the JAX-RS runtime will respond to the client with one of the following HTTP status codes:
500 (Internal Server Error) if the exception was thrown while validating a method return type
400 (Bad Request) in all other cases
尝试将 public Response test(@FormParam("test-param") @NotNull String testParam, String body)
代替 public Response test(@NotNull @FormParam("test-param") String testParam, String body)
意味着 @NotNull
之后 @FormParam("test-param")
我刚刚偶然发现了答案。 Jersey User Guide 的第 18 章指出
Bean Validation support in Jersey is provided as an extension module and needs to be mentioned explicitly in your pom.xml file (in case of using Maven):
我之前只是错过了这个,尽管如果我收到某种警告会有所帮助。