Guice configuration error: Could not find a suitable constructor
Guice configuration error: Could not find a suitable constructor
我正在使用 GUICE 为使用 Dropwizard 的 RESTful API 构建进行依赖注入。这是我得到的错误:
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.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
class:
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
}
我尝试的一件事是在我的 VisitorParams
class 中添加一个构造函数,如下所示:
public VisitorParams () {}
当我这样做时,我收到有关某些变量可能尚未初始化的错误。
那么我在这里做错了什么导致这个配置错误?我对使用 Guice 和 Dropwizard 还很陌生,所以如果您需要任何其他信息,请告诉我。谢谢!
阅读消息:VisitorParams
没有零参数构造函数,或用 @Inject
注释的构造函数。
在构造函数中添加@Inject
:
@Inject
@JsonCreator
public VisitorParams ( ...
我最终从 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
}
我正在使用 GUICE 为使用 Dropwizard 的 RESTful API 构建进行依赖注入。这是我得到的错误:
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.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
class:
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
}
我尝试的一件事是在我的 VisitorParams
class 中添加一个构造函数,如下所示:
public VisitorParams () {}
当我这样做时,我收到有关某些变量可能尚未初始化的错误。
那么我在这里做错了什么导致这个配置错误?我对使用 Guice 和 Dropwizard 还很陌生,所以如果您需要任何其他信息,请告诉我。谢谢!
阅读消息:VisitorParams
没有零参数构造函数,或用 @Inject
注释的构造函数。
在构造函数中添加@Inject
:
@Inject
@JsonCreator
public VisitorParams ( ...
我最终从 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
}