2016-04-12 39 views
0

私は、Dropwizardを使用してRESTfulなAPIビルドのために依存性注入にGUICEを使用しています。これは私が取得していますエラーです。ここGuice設定エラー:適切なコンストラクタが見つかりません

com.google.inject.ConfigurationException: Guice configuration errors:

1) Could not find a suitable constructor in com.api.analytics.visitor.web.VisitorParams. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private. at com.api.analytics.visitor.web.VisitorParams.class(VisitorParams.java:27) while locating com.api.analytics.visitor.web.VisitorParams for parameter 0 at com.api.analytics.visitor.web.v1.VisitorResource.(VisitorResource.java:68) while locating com.api.analytics.visitor.web.v1.VisitorResource

は私のリソースは、セットアップがどのようにある:私は試して追加されたなかった

package com.api.analytics.visitor.web; 

//imports 

public class VisitorParams { 
    private final Optional<Long> start; 
    private final Optional<Long> end; 
    private final Set<ActivityType> activityTypes; 
    private final Optional<Integer> limit; 
    private final boolean reversed; 
    private final Set<Long> vids; 

    @JsonCreator 
    public VisitorParams (@JsonProperty("start") Optional<Long> start, 
         @JsonProperty("end") Optional<Long> end, 
         @JsonProperty("type") Optional<Set<ActivityType>> activityTypes, 
         @JsonProperty("limit") Optional<Integer> limit, 
         @JsonProperty("reversed") @DefaultValue("false") boolean reversed, 
         @JsonProperty("vid") Optional<Set<Long>> vids) { 
    this.start = start; 
    this.end = end; 
    this.activityTypes = activityTypes.or(Collections.emptySet()); 
    this.limit = limit; 
    this.reversed = reversed; 
    this.vids = vids.or(Collections.emptySet()); 
    } 

    public Optional<Long> getStart() { 
    return this.start; 
    } 

    //other getters 
} 

一つのこと:ここで

package com.api.analytics.visitor.web.v1; 

//imports 

@Path("/visitor") 
@Produces({MediaType.APPLICATION_JSON, ExtraMediaTypes.PROTOBUF}) 
@Consumes(MediaType.APPLICATION_JSON) 
public class VisitorResource { 
    private final ContactsManager contactsManager; 
    private final ActivityFetcher.Provider activityFetcherProvider; 
    private final VisitSourceMapper visitSourceMapper; 
    private final TimeZoneClient timeZoneClient; 
    private final GatesClient gatesClient; 
    private final Value<Integer> smallScanLimit; 
    private final Provider<Integer> portalIdProvider; 
    private final VisitorParams visitorParams; 

    @Inject 
    public VisitorResource(@Bind({Bind.Params.QUERY}) VisitorParams visitorParams, 
         ContactsManager contactsManager, 
         ActivityFetcher.Provider activityFetcherProvider, 
         VisitSourceMapper visitSourceMapper, 
         TimeZoneClient timeZoneClient, 
         GatesClient gatesClient, 
         @Named("analytics.activities.fetch.small.scan.limit") Value<Integer> smallScanLimit, 
         @StashedHubId Provider<Integer> portalIdProvider) { 
    this.contactsManager = contactsManager; 
    this.activityFetcherProvider = activityFetcherProvider; 
    this.visitSourceMapper = visitSourceMapper; 
    this.timeZoneClient = timeZoneClient; 
    this.gatesClient = gatesClient; 
    this.smallScanLimit = smallScanLimit; 
    this.portalIdProvider = portalIdProvider; 
    this.visitorParams = visitorParams; 
    } 

    @Timed 
    @GET 
    @Path("/{identity}/activities") 
    public List<Activity> getActivitiesGet(@PathParam("identity") String identity) throws Exception { 
    return getActivities(identity); 
    } 

    //other methods 
} 

は私VisitorParamsクラスです私のVisitorParamsクラスのコンストラクタは次のようになります:

public VisitorParams() {} 

私が行ったときに、変数が初期化されていない可能性があるというエラーが表示されました。

この設定エラーの原因は何ですか?私はGuiceとDropwizardを使い慣れているので、他の情報が必要な場合はお知らせください。ありがとう!

答えて

-1

私はVisitorResourceコンストラクタからVisitorParams visitorParamsを削除し、そのような個々のメソッドに移動することになった:

//imports 

@Path("/visitor") 
@Produces({MediaType.APPLICATION_JSON, ExtraMediaTypes.PROTOBUF}) 
@Consumes(MediaType.APPLICATION_JSON) 
public class VisitorResource { 
    //other variables 
    private final VisitorParams visitorParams; 

    @Inject 
    public VisitorResource(/*other variables*/) { 
    this.contactsManager = contactsManager; 
    this.activityFetcherProvider = activityFetcherProvider; 
    this.visitSourceMapper = visitSourceMapper; 
    this.timeZoneClient = timeZoneClient; 
    this.gatesClient = gatesClient; 
    this.smallScanLimit = smallScanLimit; 
    this.portalIdProvider = portalIdProvider; 
    //removed visitorParams here 
    } 

    @Timed 
    @GET 
    @Path("/{identity}/activities") 
    //moved it to the methods 
    public List<Activity> getActivitiesGet(@PathParam("identity") String identity, @Bind({Bind.Params.QUERY}) VisitorParams visitorParams) throws Exception { 
    this.visitorParams = visitorParams; 
    return getActivities(identity); 
    } 

    //other methods 
} 
1

メッセージを読んでください:VisitorParamsには、引数なしのコンストラクタ、または@Injectで注釈されたコンストラクタがありません。

コンストラクタに@Injectを追加します。

@Inject 
@JsonCreator 
public VisitorParams (... 
+0

を私がしようとした言及するのを忘れてしまいました同様に、同じエラーメッセージと追加のエラーメッセージが表示されます。 –

関連する問題