Dropwizard 仅在@POST 上验证字段
Dropwizard validate a field only on @POST
我有这些需要保护的只读字段,例如密码。假设我们有一个用户对象:
public class User {
@NotEmpty
@Size(max = 100)
private String name;
@NotEmpty
private String username;
@NotEmpty
@Email
private String email;
private String password;
@JsonIgnore
public String getPassword() {
return password;
}
@JsonProperty
public void setPassword(String password) {
this.password = password;
}
}
所以,这很好用;因为我可以 get/post/put 任何我想要的东西,但我永远不会收到密码。但我也想确保第一次发布时,密码不能为空。如果我这样做
@NotEmpty
private string password;
如果我不想更改此用户的密码,那么我的 PUT(编辑)请求将失败并出现验证错误。
我能想到的解决方案有两种:
1- 继承用户 class,创建一个特殊的 class 只为 POST 可以在 getter.
public static class Create extends User {
@NotEmpty
@Override
public String getPassword() {
return password;
}
}
这应该可以正常工作,但不适用于我的代码库,因为它已经在 CRUD 资源上大量使用继承。对于这种方法,我需要打破和复制很多东西。
2- 处理资源验证 class:
public class UserResource {
@POST
public User createUser(User user) {
if(user.getPassword().isEmpty()) {
throw new ConstraintValidation....();
}
}
}
完成任务,但不是很漂亮。特别是因为我有 5-10 个这样的东西。
还有其他选择吗?
除了球衣注入和自定义验证器,我发现了一个更酷、更简单、更流畅的解决方案,我认为你可能会感兴趣,Natan:
DW 改为支持验证组。有了这些,您可以根据您的方法决定注释什么,而无需验证知道该方法。您可以在此处阅读更多相关信息:
http://www.dropwizard.io/0.9.0/docs/manual/validation.html
(顺便说一句,我在示例中使用的是 RC 版本)。
让我们开始吧:
@Path("/hello/world")
@Produces(MediaType.APPLICATION_JSON)
public class HelloWorldResource {
@POST
@Path("/v1")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response test(@Valid @Validated(V1Check.class) User user) {
// checks only username
System.out.println(user.getName());
System.out.println(user.getPassword());
return Response.ok().build();
}
@POST
@Path("/v2")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response test2(@Valid @Validated(V2Check.class) User user) {
// checks both
System.out.println(user.getName());
System.out.println(user.getPassword());
return Response.ok().build();
}
}
这是资源 class。看看我是如何用@Validated 注释这两种方法的。这告诉 DW 确切地验证什么。
这是我的用户 class:
public class User {
@JsonProperty("name")
private String name;
@JsonProperty("password")
private String password;
@NotEmpty(message="other message", groups= {V2Check.class} )
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@NotEmpty(message="asd asd asd", groups= {V1Check.class, V2Check.class } )
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public interface V1Check {};
public interface V2Check {};
}
我也在 class 中嵌入了接口。现在仅检查 V2 密码。因此,对于您的 POST 方法,您需要将其添加到 Validated 注释中,而您的 get 方法可以保持 V1 检查并忽略密码。
另外,我的测试首发:
public class Starter extends Application<Configuration> {
@Override
public void run(Configuration configuration, Environment environment) throws Exception {
environment.jersey().register(HelloWorldResource.class);
}
public static void main(String[] args) throws Exception {
new Starter().run("server", "/Users/artur/dev/repo/dw-test/src/main/resources/configuration.yaml");
}
}
这是执行此操作的 DW 方法,但是您也应该能够将球衣注入添加到您的自定义验证器中。只是因为您不需要检查密码,所以似乎没有必要编写自定义验证器。
这是我的卷发。 user_json2:
{
"name" : "artur"
}
V1,不校验密码:
arturk:tmp artur$ curl -XPOST "localhost:9085/hello/world/v1/" --header "Content-Type: application/json" -d @user_json2 -v
* Trying ::1...
* Connected to localhost (::1) port 9085 (#0)
> POST /hello/world/v1/ HTTP/1.1
> Host: localhost:9085
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 19
>
* upload completely sent off: 19 out of 19 bytes
< HTTP/1.1 200 OK
< Date: Fri, 27 May 2016 09:59:22 GMT
< Content-Length: 0
<
* Connection #0 to host localhost left intact
V2,校验密码:
arturk:tmp artur$ curl -XPOST "localhost:9085/hello/world/v2/" --header "Content-Type: application/json" -d @user_json2 -v
* Trying ::1...
* Connected to localhost (::1) port 9085 (#0)
> POST /hello/world/v2/ HTTP/1.1
> Host: localhost:9085
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 19
>
* upload completely sent off: 19 out of 19 bytes
< HTTP/1.1 400 Bad Request
< Date: Fri, 27 May 2016 10:07:41 GMT
< Content-Type: text/html;charset=iso-8859-1
< Cache-Control: must-revalidate,no-cache,no-store
< Content-Length: 251
<
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 400 Bad Request</title>
</head>
<body><h2>HTTP ERROR 400</h2>
<p>Problem accessing /hello/world/v2/. Reason:
<pre> Bad Request</pre></p>
</body>
</html>
* Connection #0 to host localhost left intact
希望对您有所帮助!
干杯,
阿图尔
我有这些需要保护的只读字段,例如密码。假设我们有一个用户对象:
public class User {
@NotEmpty
@Size(max = 100)
private String name;
@NotEmpty
private String username;
@NotEmpty
@Email
private String email;
private String password;
@JsonIgnore
public String getPassword() {
return password;
}
@JsonProperty
public void setPassword(String password) {
this.password = password;
}
}
所以,这很好用;因为我可以 get/post/put 任何我想要的东西,但我永远不会收到密码。但我也想确保第一次发布时,密码不能为空。如果我这样做
@NotEmpty
private string password;
如果我不想更改此用户的密码,那么我的 PUT(编辑)请求将失败并出现验证错误。
我能想到的解决方案有两种:
1- 继承用户 class,创建一个特殊的 class 只为 POST 可以在 getter.
public static class Create extends User {
@NotEmpty
@Override
public String getPassword() {
return password;
}
}
这应该可以正常工作,但不适用于我的代码库,因为它已经在 CRUD 资源上大量使用继承。对于这种方法,我需要打破和复制很多东西。
2- 处理资源验证 class:
public class UserResource {
@POST
public User createUser(User user) {
if(user.getPassword().isEmpty()) {
throw new ConstraintValidation....();
}
}
}
完成任务,但不是很漂亮。特别是因为我有 5-10 个这样的东西。
还有其他选择吗?
除了球衣注入和自定义验证器,我发现了一个更酷、更简单、更流畅的解决方案,我认为你可能会感兴趣,Natan:
DW 改为支持验证组。有了这些,您可以根据您的方法决定注释什么,而无需验证知道该方法。您可以在此处阅读更多相关信息:
http://www.dropwizard.io/0.9.0/docs/manual/validation.html
(顺便说一句,我在示例中使用的是 RC 版本)。
让我们开始吧:
@Path("/hello/world")
@Produces(MediaType.APPLICATION_JSON)
public class HelloWorldResource {
@POST
@Path("/v1")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response test(@Valid @Validated(V1Check.class) User user) {
// checks only username
System.out.println(user.getName());
System.out.println(user.getPassword());
return Response.ok().build();
}
@POST
@Path("/v2")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response test2(@Valid @Validated(V2Check.class) User user) {
// checks both
System.out.println(user.getName());
System.out.println(user.getPassword());
return Response.ok().build();
}
}
这是资源 class。看看我是如何用@Validated 注释这两种方法的。这告诉 DW 确切地验证什么。
这是我的用户 class:
public class User {
@JsonProperty("name")
private String name;
@JsonProperty("password")
private String password;
@NotEmpty(message="other message", groups= {V2Check.class} )
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@NotEmpty(message="asd asd asd", groups= {V1Check.class, V2Check.class } )
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public interface V1Check {};
public interface V2Check {};
}
我也在 class 中嵌入了接口。现在仅检查 V2 密码。因此,对于您的 POST 方法,您需要将其添加到 Validated 注释中,而您的 get 方法可以保持 V1 检查并忽略密码。
另外,我的测试首发:
public class Starter extends Application<Configuration> {
@Override
public void run(Configuration configuration, Environment environment) throws Exception {
environment.jersey().register(HelloWorldResource.class);
}
public static void main(String[] args) throws Exception {
new Starter().run("server", "/Users/artur/dev/repo/dw-test/src/main/resources/configuration.yaml");
}
}
这是执行此操作的 DW 方法,但是您也应该能够将球衣注入添加到您的自定义验证器中。只是因为您不需要检查密码,所以似乎没有必要编写自定义验证器。
这是我的卷发。 user_json2:
{
"name" : "artur"
}
V1,不校验密码:
arturk:tmp artur$ curl -XPOST "localhost:9085/hello/world/v1/" --header "Content-Type: application/json" -d @user_json2 -v
* Trying ::1...
* Connected to localhost (::1) port 9085 (#0)
> POST /hello/world/v1/ HTTP/1.1
> Host: localhost:9085
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 19
>
* upload completely sent off: 19 out of 19 bytes
< HTTP/1.1 200 OK
< Date: Fri, 27 May 2016 09:59:22 GMT
< Content-Length: 0
<
* Connection #0 to host localhost left intact
V2,校验密码:
arturk:tmp artur$ curl -XPOST "localhost:9085/hello/world/v2/" --header "Content-Type: application/json" -d @user_json2 -v
* Trying ::1...
* Connected to localhost (::1) port 9085 (#0)
> POST /hello/world/v2/ HTTP/1.1
> Host: localhost:9085
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 19
>
* upload completely sent off: 19 out of 19 bytes
< HTTP/1.1 400 Bad Request
< Date: Fri, 27 May 2016 10:07:41 GMT
< Content-Type: text/html;charset=iso-8859-1
< Cache-Control: must-revalidate,no-cache,no-store
< Content-Length: 251
<
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 400 Bad Request</title>
</head>
<body><h2>HTTP ERROR 400</h2>
<p>Problem accessing /hello/world/v2/. Reason:
<pre> Bad Request</pre></p>
</body>
</html>
* Connection #0 to host localhost left intact
希望对您有所帮助!
干杯,
阿图尔