Dropwizard is using Jersey 用于 HTTPJava POJO 编组。您可以将 Jersey @*Param(@FormParam、@QueryParam 等)中的各种注释用于某些参数。
如果您需要在 Java POJO 之间使用 map/marshall,请查看test cases in Dropwizard:
@Path("/valid/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class ValidatingResource {
@POST
@Path("foo")
@Valid
public ValidRepresentation blah(@NotNull @Valid ValidRepresentation representation, @QueryParam("somethingelse") String xer) {
return new ValidRepresentation();
}
这定义了一个响应 HTTP POST 方法的 API 端点,该方法需要 ValidRepresentation 对象和“somethingelse”作为 HTTP 方法查询参数。端点仅在提供 JSON 参数时才会响应,并且仅返回 JSON 对象(类级别的@Produces 和@Consumes)。 @NotNull 要求该对象是强制调用才能成功,@Valid 指示 Dropwizard 调用 Hibernate 验证器以在调用端点之前验证该对象。
ValidRepresentation 类是here:
package io.dropwizard.jersey.validation;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.validator.constraints.NotEmpty;
public class ValidRepresentation {
@NotEmpty
private String name;
@JsonProperty
public String getName() {
return name;
}
@JsonProperty
public void setName(String name) {
this.name = name;
}
}
POJO 使用 Jackson 注释来定义这个对象的 JSON 表示应该是什么样子。 @NotEmtpy 是来自 Hibernate 验证器的注解。
Dropwizard、Jersey 和 Jackson 负责处理细节。所以对于基本的东西,这就是你所需要的。