Jersey 2とJAX-RSを初めて使用しているので、おそらく何か不足しています。 私がしようとしているのは、休憩サービス開発のコーディングスタイルを定義するテストプログラムです。 このテストは、Javaで書かれ、JERSEY 2.22.2、JDK 1.8.31、MOXY AS JSON Providerを使用しています。Jersey 2でEntityFilteringFeatureとSelectableEntityFilteringFeatureを使用するときの問題
LIST/DETAILをサポートするGETメソッドを持つリソースを定義しました。私のPOJOのサイズのために、私はいくつかのフィルタを使用し、すべてがうまくいきました。最初のテストの後
// 1) First of all I defined the annotation.
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@EntityFiltering
public @interface MyDetailView {
public static class Factory extends AnnotationLiteral<MyDetailView>
implements MyDetailView {
private Factory() {
}
public static MyDetailView get() {
return new Factory();
}
}
// 2) Once defined the annotation, I used to
// programmaticaly exclude the list of subItems in the response...
@XmlRootElement
public class MyPojo {
...
//*** THIS SHOULD BE FILTERED IF THE ANNOTATION IS NOT SPECIFIED IN THE RESPONSE ***
@MyDetailView
private List<SubItem> subItems = new ArrayList<SubItem>();
public List<SubItem> getSubItems() {
return subItems;
}
public void setSubItems(List<SubItem> subItems) {
this.subItems = subItems;
}
}
// 3) I registered the EntityFilteringFeature
public class ApplicationConfig extends ResourceConfig {
public ApplicationConfig() {
....
register(EntityFilteringFeature.class);
}
// 4) Finally, I wrote the code to include/exclude the subItems
/*
The Resource class has getCollection() and getItem() methods...
getCollection() adds the annotation only if filterStyle="detail"
getItem() always add the annotation
*/
@Path(....)
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class MyResource extends SecuredResource {
//filterStyle -> "detail" means MyDetailAnnotation
@GET
public Response getCollection(
@QueryParam("filterStyle") String filterStyle,
@Context UriInfo uriInfo) {
//THIS CODE AFFECTS THE RESPONSE
boolean detailedResponse = "detail".equals(filterStyle);
Annotation[] responseAnnotations = detailedResponse
? new Annotation[0]
: new Annotation[]{MyDetailView.Factory.get()};
//pojo collection...
MyPagedCollection myCollection = new MyPagedCollection();
//.....
ResponseBuilder builder = Response.ok();
return builder.entity(myCollection, responseAnnotations).build();
}
@GET
@Path("/{id}")
public Response getItem(@PathParam("{id}") String idS, @Context UriInfo uriInfo) {
MyPOJO pojo = ...
Annotation[] responseAnnotations = new Annotation[]{MyDetailView.Factory.get()};
return Response.ok().entity(pojo, responseAnnotations).build();
}
}
、私は、クライアントが詳細に特定のフィールドを求めることができるようにSelectableEntityFilteringFeatureを使用しようとしましたので、私は
public class ApplicationConfig extends ResourceConfig {
public ApplicationConfig() {
....
register(EntityFilteringFeature.class);
register(SelectableEntityFilteringFeature.class);
property(SelectableEntityFilteringFeature.QUERY_PARAM_NAME, "fields");
}
ApplicationConfig
を変更し、私は」 "fields" QueryParamをResource getItem()メソッドに追加してください。@GET
@Path("/{id}")
public Response getDetail(@PathParam({id}) String id,
@QueryParam("fields") String fields,
@Context UriInfo uriInfo) {
....
しかし、SelectableEntityFilteringFeatureクラスを登録していれば、EntityFilteringFeatureクラスは機能しなくなりました。 Resourceメソッドの1つに "fields"パラメータを追加しようとしましたが、それは完全に機能しました。しかし、MyDetailAnnotationは全く役に立たなかった。
1)なぜ登録SelectableEntityFilteringFeature機能の両方がEntityFilteringFeatureを破る:
は私が今の質問をDynamicFeaturepublic class MyDynamicFeature implements DynamicFeature {
@Override
public void configure(ResourceInfo resourceInfo, FeatureContext context) {
if ("MyResource".equals(resourceInfo.getResourceClass().getSimpleName())
&& "getItem".equals(resourceInfo.getResourceMethod().getName())) {
//*** IS THE CORRECT WAY TO BIND A FEATURE TO A METHOD? ***
//
context.register(SelectableEntityFilteringFeature.class);
context.property(SelectableEntityFilteringFeature.QUERY_PARAM_NAME, "fields");
}
}
を使用して登録しようとしましたか?
2)DynamicFeatureインターフェイスでメソッドをフィーチャにバインドする正しい方法は何ですか?
ありがとうございます。 これは私の最初のStack Overflowへの投稿です。ルールに不満を書きました。
ありがとうございました。しかし、これは私がやったことであり、うまくいきました(唯一の違いは、ResourceConifgサブクラスでプロパティを指定しなかったことです。私はResourceメソッドにアノテーションを追加しました)。私の目標は、DetailメソッドでSelectableEntityFilteringFeatureを使用することです(明確にするために、QueryParamの "fields"を使用して、別のメソッドのシグネチャをコードに追加しました)。その間、私はgetCollection()をそのまま残したい(投稿したリンクで説明されているとおり、MyDetailViewアノテーションを使ってレスポンスをフィルタリングする)。問題は、変更されていないgetCollection()がフィルタリングを停止したことです。 –
ポスターはEntityFilteringFeatureとSelectableEntityFilteringFeatureの両方を使いたいので、これは本当の答えではありません。アノテーションベースのEntityFilteringFeatureのみを使用する方法を説明します – ChrisO