2016-04-12 7 views
0

Ich verwende GUICE für die Abhängigkeitsinjektion für einen RESTful-API-Build mit Dropwizard. Dies ist der Fehler Ich erhalte:Guice-Konfigurationsfehler: Konnte keinen geeigneten Konstruktor finden

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

Hier ist, wie meine Ressource-Setup ist:

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 
} 

Hier ist meine VisitorParams Klasse:

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 
} 

Eine Sache, die ich ausprobiert habe wurde hinzugefügt ein Konstruktor in meiner VisitorParams Klasse wie folgt:

public VisitorParams() {} 

Wenn ich das gemacht habe, bekomme ich Fehler, wie einige Variablen möglicherweise nicht initialisiert worden sind.

Was mache ich hier falsch, um diesen Konfigurationsfehler zu verursachen? Ich bin ziemlich neu bei der Verwendung von Guice und Dropwizard, also lassen Sie es mich wissen, wenn Sie weitere Informationen benötigen. Vielen Dank!

Antwort

-1

ich die VisitorParams visitorParams vom VisitorResource Konstruktor Entfernen beendet und es zu den einzelnen Methoden wie so bewegend:

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

Lesen Sie die Nachricht: VisitorParams hat keinen Null-Arg-Konstruktor oder einen Konstruktor, der mit @Inject annotiert ist.

hinzufügen @Inject an den Konstruktor:

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

ich vergaß zu erwähnen, ich versucht hatte, auch das, und ich habe die gleiche Fehlermeldung und zusätzliche Fehlermeldungen. –

Verwandte Themen