hstaudacher / osgi-jax-rs-connector

An OSGi - JAX-RS 2.0 Connector, software repository available on the link below
http://hstaudacher.github.io/osgi-jax-rs-connector
Other
190 stars 98 forks source link

Add support for Jackson JAX-RS provider #127

Open BryanHunt opened 8 years ago

BryanHunt commented 8 years ago

It looks like the Jackson JAX-RS provider is included in the jersey-all.jar so I think it is just a matter of registering the provider as a service component.

BryanHunt commented 8 years ago

Here is all that is needed. I'd do a PR, but still can't figure out the build.

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;

public class Activator implements BundleActivator
{
  private ServiceRegistration registration;

  @Override
  public void start( BundleContext context ) throws Exception {
    JacksonJsonProvider service = new JacksonJsonProvider();
    registration = context.registerService( JacksonJsonProvider.class.getName(), service, null );
  }

  @Override
  public void stop( BundleContext context ) throws Exception {
    if( registration != null ) {
      registration.unregister();
    }
  }
}
hstaudacher commented 8 years ago

@BryanHunt I think this would mean that the Jersey bundle needs to export the Jackson packages, right? Do you think this is a good thing? I have doubts...

BryanHunt commented 8 years ago

Users could always add the Jackson bundles externally if you want to keep those private.

BryanHunt commented 8 years ago

Forgot to mention that not all of the needed Jackson bundles are included in the jersey.all.jar, you need external Jackson bundles to get this to work.

hstaudacher commented 8 years ago

So, I'm happy to add a Jackson Provider but it should be self contained and useable like the gson provider. It has it's own feature including it's dependencies. If you can provide such a provider we will add it.

BryanHunt commented 8 years ago

After some additional research, here is the proper way to include Jackson:

import org.glassfish.jersey.jackson.JacksonFeature;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

public class Activator implements BundleActivator
{
  @SuppressWarnings("rawtypes")
  private ServiceRegistration registration;

  @Override
  public void start( BundleContext context ) throws Exception {
    JacksonFeature feature = new JacksonFeature();
    registration = context.registerService( JacksonFeature.class.getName(), feature, null );
  }

  @Override
  public void stop( BundleContext context ) throws Exception {
    if( registration != null ) {
      registration.unregister();
    }
  }
}
hstaudacher commented 8 years ago

Thanks Bryan, will add this as a provider for the next release.

BryanHunt commented 8 years ago

Thanks. BTW, if there were contributing docs that explained importing the projects into Eclipse, fixing the missing Maven connector, setting up the target platform, adding a provider, running tests, etc, that would go a long way to increasing the probability of a PR.

dzmitry-kankalovich commented 8 years ago

@BryanHunt thanks for the tip with JacksonFeature, that helped me to enable JSON support in POST calls. However during deserialization from JSON to POJO it looks like Jackson annotations (for example JsonProperty) are being ignored. I am using com.fasterxml.jackson.annotation package (which is for Jackson 2) and it should be fine with jersey-all:2.22.2, however those annotations simply do nothing. Do you know why?

dzmitry-kankalovich commented 8 years ago

I can only guess that this issue is caused by jersey-all bundle using it's internal com.fasterxml.jackson.annotation package which had not being exposed.

At this point probably better to fallback to Gson provider.

BryanHunt commented 8 years ago

@dzmitry-kankalovich Have you tried using the jersey-min bundle?

nschroeter commented 8 years ago

Adding jackson support would be really awesome - makes it possible to use the wonderful mongojack library.

@BryanHunt I can confirm, that some jackson annotations are not working (in fact, most of them..) However, if i annotate a field with

public class Example {
    //some other fields here
    @JsonIgnore(value=true)
    public String title;
}

the annotation is processed, but incorrectly: instead of ommitting the field, it just sets the value to null ("content":null)

No idea why, i need to investigate this weird behaviour.

BTW, this is with com.eclipsesource.jaxrs.jersey-all/com.eclipsesource.jaxrs.jersey-all, version 2.22.2

nschroeter commented 8 years ago

@dzmitry-kankalovich You were right, this issue is caused by jersey-all bundle using it's internal com.fasterxml.jackson.annotation package which had not being exposed.

There are two workarounds:

1) don't use the jersey-all bundle at all and add the jersey dependencies manually (lots of bundles!) 2) use the jersey-min bundle and a few missing dependencies manually. If you do this, your bndrun file should look include

-runbundles: \ com.eclipsesource.jaxrs.jersey-min;version='[2.22.2,2.22.3)',\ com.eclipsesource.jaxrs.publisher;version='[5.3.1,5.3.2)',\ com.fasterxml.classmate;version='[1.3.1,1.3.2)',\ com.fasterxml.jackson.core.jackson-annotations;version='[2.7.4,2.7.5)',\ com.fasterxml.jackson.core.jackson-core;version='[2.7.4,2.7.5)',\ com.fasterxml.jackson.core.jackson-databind;version='[2.7.4,2.7.5)',\ com.fasterxml.jackson.jaxrs.jackson-jaxrs-base;version='[2.7.4,2.7.5)',\ com.fasterxml.jackson.jaxrs.jackson-jaxrs-json-provider;version='[2.7.4,2.7.5)',\ javax.annotation-api;version='[1.2.0,1.2.1)',\ javax.inject;version='[1.0.0,1.0.1)',\ javax.ws.rs-api;version='[2.0.1,2.0.2)',\ org.apache.felix.configadmin;version='[1.8.8,1.8.9)',\ org.apache.felix.eventadmin;version='[1.4.4,1.4.5)',\ org.apache.felix.gogo.command;version='[0.14.0,0.14.1)',\ org.apache.felix.gogo.runtime;version='[0.16.2,0.16.3)',\ org.apache.felix.gogo.shell;version='[0.10.0,0.10.1)',\ org.apache.felix.http.jetty;version='[3.2.0,3.2.1)',\ org.apache.felix.http.servlet-api;version='[1.1.2,1.1.3)',\ org.apache.felix.log;version='[1.0.1,1.0.2)',\ org.apache.felix.scr;version='[2.0.0,2.0.1)',\ org.glassfish.jersey.bundles.repackaged.jersey-guava;version='[2.22.2,2.22.3)',\ org.glassfish.jersey.ext.jersey-entity-filtering;version='[2.22.2,2.22.3)',\ org.glassfish.jersey.media.jersey-media-json-jackson;version='[2.22.2,2.22.3)',\ osgi.promise;version='[6.0.0,6.0.1)'

@hstaudacher I can see that org.glassfish.jersey.bundles.repackaged.jersey-guava is already included in jersey-min, is there a reason why the packages are not exported?