Guice配置错误:找不到合适的构造函数

时间:2016-04-12 20:34:56

标签: java guice dropwizard

我使用GUICE进行依赖注入,使用Dropwizard进行RESTful API构建。这是我得到的错误:

  

com.google.inject.ConfigurationException:Guice配置错误:

     

1)无法找到合适的构造函数   com.api.analytics.visitor.web.VisitorParams。课必须有   用@Inject或a注释的一个(也是唯一一个)构造函数   零私有构造函数。在   com.api.analytics.visitor.web.VisitorParams.class(VisitorParams.java:27)   找到com.api.analytics.visitor.web.VisitorParams       参数0在com.api.analytics.visitor.web.v1.VisitorResource。(VisitorResource.java:68)   找到位置   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的新手,所以如果您需要任何其他信息,请告诉我。谢谢!

2 个答案:

答案 0 :(得分:1)

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

@Inject添加到构造函数:

@Inject
@JsonCreator
public VisitorParams ( ...

答案 1 :(得分:-1)

我最终从VisitorParams visitorParams构造函数中移除VisitorResource并将其移动到各个方法,如下所示:

//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
}