Guice Injection into Google Cloud Endpoints Transformer

431 views Asked by At

I have a Google Cloud Endpoints application successfully working with Guice.

I wish to inject a Singleton into an Api Transformer.

Let's say I wish to transform Something into SomethingElse, where Something declares it's transformer to be:

import com.google.api.server.spi.config.Transformer;
import com.google.inject.Inject;
import com.google.inject.Singleton;

@Singleton
public class MyApiTransformer
    implements Transformer<Something, SomethingElse> {

private MySingleton singleton;

@Inject
public MyApiTransformer(MySingleton singleton) {
    this.singleton = singleton;
}
@Override
public Something transformFrom(SomethingElse somethingElse) {
    return singleton.something(somethingElse);
}

@Override
public SomethingElse transformTo(Something something) {
    return singleton.somethingElse(something);
}

}

Notice that I wish to delegate transformation to my Guice singleton. When I try the above transformer I get the following error:

java.io.IOException: com.google.appengine.repackaged.org.codehaus.jackson.map.JsonMappingException: Failed to instantiate custom serializer MyApiTransformer, constructors not found: [(interface java.lang.reflect.Type), (class java.lang.Class), ()]

It seems that Guice is not providing the ApiTransformers so Jackson does not know how to instantiate the class without a default constructor.

How can Guice inject the Singleton into the ApiTransformer?

1

There are 1 answers

4
Aaron Roller On

Give your Transformer a default constructor and do static Injection:

import com.google.api.server.spi.config.Transformer;
import com.google.inject.Inject;

public class MyApiTransformer
    implements Transformer<Something, SomethingElse> {

@Inject
private static MySingleton singleton;

public MyApiTransformer() {

}
@Override
public Something transformFrom(SomethingElse somethingElse) {
    return singleton.something(somethingElse);
}

@Override
public SomethingElse transformTo(Something something) {
    return singleton.somethingElse(something);
}

}

in your Module:

public class MyModule
    extends AbstractModule {

@Override
protected void configure() {

    requestStaticInjection(MyApiTransformer.class);
}

}