guice-jersey icon indicating copy to clipboard operation
guice-jersey copied to clipboard

"application/json" provider not binded by default

Open TryKote opened this issue 4 years ago • 1 comments

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 :)

TryKote avatar Sep 04 '19 10:09 TryKote