Java 子资源情况下的JAX-RS CXF交叉参数验证

Java 子资源情况下的JAX-RS CXF交叉参数验证,java,rest,validation,jax-rs,cxf,Java,Rest,Validation,Jax Rs,Cxf,我正在基于JAX-RSCXF构建REST服务,并寻找一种方法来验证一对相互依赖的交叉参数。最棘手的部分是,我正在处理子资源,而我的方法并没有在方法级别验证所有参数 让我们以一些简单的实体为例 顶级API是CustomerResource: POST /customer -- add a new customer GET /customer/{customerId} -- get the customer by id POST /customer/{customerId}/order -

我正在基于JAX-RSCXF构建REST服务,并寻找一种方法来验证一对相互依赖的交叉参数。最棘手的部分是,我正在处理子资源,而我的方法并没有在方法级别验证所有参数

让我们以一些简单的实体为例

顶级API是CustomerResource:

POST /customer    -- add a new customer
GET  /customer/{customerId} -- get the customer by id
POST /customer/{customerId}/order -- put a new order for the customer with {customerId}
子资源API是OrderResource:

POST /customer    -- add a new customer
GET  /customer/{customerId} -- get the customer by id
POST /customer/{customerId}/order -- put a new order for the customer with {customerId}
尸体在哪里

{
    "itemId" : "123",
    "discountId" : "456"
}
我需要验证具有{customerId}的客户是否拥有属于他的折扣{discountId}。

@Path("/customer")
public interface CustomerResource {
    ...
    @Path("/{customerId}/order")
    OrderResource ordersOf(@PathParam("customerId") String customerId);
    ...
}

public interface OrderResource {
    ...
    @POST
    @Path("/")
    @Consumes(MediaType.APPLICATION_JSON)
    Order apply(@Valid OrderRequest orderRequest);
    ...
}

public class OrderRequest {
    private String itemId;
    private String discountId;
    ...
}
验证每个独立参数是否为@NotNull或是否存在很容易。 但是我的特殊折扣验证器在验证时应该具有和customerId和折扣ID,以便从DB读取正确的对象并执行检查

当然,我可以稍后在服务层上进行验证,但目前我正在努力使服务代码尽可能简洁

还请注意,更改API或更新DB关系的成本太高