我想做一個檢查,看看我們是否已經「關閉」了某個特定的端點。目前它通過一個ContainerRequestFilter實現。但是由於我無法直接在控制器/資源級別掛接到過濾器(據我所知),我所能做的最好的方法是從過濾器中拋出異常,然後將其返回給客戶端,我寧願客戶端看到格式化的json錯誤響應而不是堆棧跟蹤。是否有可能對球衣REST方法施加約束?
我實現以下約束接口和類:
@NotNull
@Target({METHOD, FIELD, ANNOTATION_TYPE, PARAMETER})
@Retention(RUNTIME)
@Constraint(validatedBy = MethodNotImplementedImpl.class)
@Documented
public @interface MethodNotImplemented {
String message() default "{Default message}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
public class MethodNotImplementedImpl implements ConstraintValidator<MethodNotImplemented, UriInfo> {
public void initialize(MethodNotImplemented constraintAnnotation) {logger.debug("init called");}
public boolean isValid(UriInfo uriInfo, ConstraintValidatorContext constraintContext) {
logger.debug("validate called");
return true;
}
}
我想有那麼每個休息方法執行約束,但約束不點火。
@Validated
@Path("/people")
public class PeopleController extends BaseController {
@GET
@Path("/{peopleId}")
@Produces(MediaType.APPLICATION_JSON)
public Response getPeople(@MethodNotImplemented @Context UriInfo uriInfo,
@PathParam("peopleId") String peopleId,
@DefaultValue("") @QueryParam("fields") String partialResponseFields) {
....
所有編譯和部署都很好,但是在調用端點時不會調用init或isValid方法。我在應用程序中的POJO上設置了其他方法級別約束,並且在調用方法時它們會自動觸發。我正在嘗試做什麼?如果是這樣,我做錯了什麼?
您使用的是Jersey 1.x還是Jersey 2.x? –
目前我們正在使用球衣1.x,計劃轉移到2.x. – Tracy