gpt4 book ai didi

java - Guice配置错误: Could not find a suitable constructor

转载 作者:太空宇宙 更新时间:2023-11-04 12:43:29 27 4
gpt4 key购买 nike

我正在使用 GUICE 进行依赖项注入(inject),以使用 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 类:

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 类中添加一个构造函数,如下所示:

public VisitorParams () {}

当我这样做时,我收到有关某些变量可能尚未初始化的错误。

那么我在这里做错了什么导致这个配置错误?我对使用 Guice 和 Dropwizard 还很陌生,所以如果您需要任何其他信息,请告诉我。谢谢!

最佳答案

阅读消息:VisitorParams 没有零参数构造函数,或用 @Inject 注解的构造函数。

@Inject添加到构造函数:

@Inject
@JsonCreator
public VisitorParams ( ...

关于java - Guice配置错误: Could not find a suitable constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36583554/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com