logzio / guice-jersey

Guice module for starting Jetty based rest server with Jersey
Apache License 2.0
40 stars 11 forks source link

"application/json" provider not binded by default #18

Open TryKote opened 5 years ago

TryKote commented 5 years ago

Hi! I'm not sure, that is bug, but by default JacksonJsonProvider.class not registrated in JerseyConfiguration module.

I have situation: Simple controller:

@Path("/version")
public class VersionController {
    private ApplicationInfo applicationInfo;

    @Inject
    public VersionController(ApplicationInfo applicationInfo) {
        this.applicationInfo = applicationInfo;
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public ApplicationInfo getInfo() {
        return applicationInfo;
    }
}

where ApplicationInfo is POJO binded as singleton with @Singleton annotation. I run in IDE, go to /version endpoint — all goes good. I build package with maven, run it and get error MessageBodyWriter not found for media type=application/json, type=class com.my.package.ApplicationInfo, genericType=class com.my.package.ApplicationInfo.

Other mime-types work fine.

I found solution. It's necessary to manual register JacksonJsonProvider.class in JerseyConfiguration:

        JerseyConfiguration configuration = JerseyConfiguration.builder()
                .addPackage("com.my.package")
                .registerClasses(JacksonJsonProvider.class) // <-- this
                .addPort(1234)
                .build(); 

Correct me if i'm wrong

PS: Thanks a lot for your lib. I have same troubles, you solution made my life easier :)

asafm commented 5 years ago

We didn't necessarily wanted to the change the defaults provided by Jersey it self, as it become more opinionated. Maybe some people like GSON better than Jackson. As long as you can do it in one liner, seems reasonable to me.