Giter VIP home page Giter VIP logo

jackson-jaxrs-propertyfiltering's Introduction

jackson-jaxrs-propertyfiltering

Overview

Library to enable automatic filtering of JSON responses from JAX-RS endpoints. Clients specify via query params what properties they want and the library handles filtering the response entities down to just those properties (see here for some examples). Tested with Jersey but should work with any JAX-RS implementation.

Maven dependency

To use module on Maven-based projects, use following dependency:

<dependency>
  <groupId>com.hubspot.jackson</groupId>
  <artifactId>jackson-jaxrs-propertyfiltering</artifactId>
  <version>0.8.4</version>
</dependency>

(or whatever version is most up-to-date at the moment)

Configuration

  1. Register PropertyFilteringMessageBodyWriter with your JAX-RS implementation
  2. Annotate the desired endpoints with @PropertyFiltering
  3. Profit

Yes, it's just that simple.

Usage

Let's assume you have an endpoint annotated with @PropertyFiltering that returns JSON of the form:

{
  "id": 54,
  "name": "Object",
  "child": {
    "id": 96,
    "name": "Child Object"
  }
}

If you just need the id field you can pass ?property=id which will return:

{
  "id": 54
}

Nested fields are also supported with dot-notation, so if you need the child id as well, you can pass ?property=id&property=child.id which will return:

{
  "id": 54,
  "child": {
    "id": 96
  }
}

You can also specify fields by exclusion rather than inclusion, so if you don't need the child field but want everything else you can pass ?property=!child which will return:

{
  "id": 54,
  "name": "Object"
}

jackson-jaxrs-propertyfiltering's People

Contributors

boulter avatar hs-jenkins-bot avatar jaredstehler avatar jboulter avatar jhaber avatar maddoc42 avatar snappiet avatar stevie400 avatar tpetr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jackson-jaxrs-propertyfiltering's Issues

Extend PropertyFilteringMessageBodyWriter

Hi,

Your lib is very nice. I have a suggestion to make. It will be kind if the PropertyFilteringMessageBodyWriter is extendable. I have a similar BodyWriter which filter property according to a securityContext. I'd like to merge the tow BodyWriter in a single one but I have nowhere to extend the PropertyFilteringMessageBodyWriter.

You could add an empty method addPropertiesToFilter invoked at the end of the getProperties method which could be overriden like that.

private Collection getProperties(String name) {
List values = uriInfo.getQueryParameters().get(name);

List<String> properties = new ArrayList<String>();
if (values != null) {
  for (String value : values) {
    String[] parts = value.split(",");
    for (String part : parts) {
      part = part.trim();
      if (!part.isEmpty()) {
        properties.add(part);
      }
    }
  }
}
addPropertiesToFilter(properties);
return properties;

}

protected void addPropertiesToFilter(properties) {
}

Regards,

MessageBodyWriter is not initializing

Not sure how this is supposed to work. Using 0.7.1, I am getting null writer and with 0.7.0, I am getting null json provider. This is a CXF web service. I am not using jetty.

-- beans.xml --
<jaxrs:server_` id="restContainer" address="/">
jaxrs:serviceBeans

/jaxrs:serviceBeans
jaxrs:providers


/jaxrs:providers
/jaxrs:server

<bean id="propertyFilteringMessageBodyWriter" class="com.hubspot.jackson.jaxrs.PropertyFilteringMessageBodyWriter">
</bean>
<bean id="jacksonJsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider">
</bean>

-- Exception --
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: Autowired annotation requires at least one argument: public com.hubspot.jackson.jaxrs.PropertyFilteringMessageBodyWriter()
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:281)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1057)

filtering properties form items in collection

Hi,

Apparently, the filter doesn't support the filtering of properties from a collection . Do you confirm ?

"items":[
{
"prop1":"myValue1",
"prop2":"myValue2"
},
{
"prop1":"myValue3",
"prop2":"myValue4"
}
]

PropertyFiltering doesn't work when using Jackson 2.10

Works still with 0.8.3 version, but the performance improvements in 0.8.4 seem to have broken it.

I think that PropertyFilteringJsonGenerator needs some fix as it's extending JsonGeneratorDelegate and that one has changed in Jackson 2.10.

Here's the stack trace I'm seeing thrown:

com.fasterxml.jackson.core.JsonGenerationException: Can not start an array, expecting field name (context: Object)
	at com.fasterxml.jackson.core.JsonGenerator._reportError(JsonGenerator.java:2080)
	at com.fasterxml.jackson.core.json.JsonGeneratorImpl._reportCantWriteValueExpectName(JsonGeneratorImpl.java:248)
	at com.fasterxml.jackson.core.json.UTF8JsonGenerator._verifyValueWrite(UTF8JsonGenerator.java:1132)
	at com.fasterxml.jackson.core.json.UTF8JsonGenerator.writeStartArray(UTF8JsonGenerator.java:331)
	at com.fasterxml.jackson.core.JsonGenerator.writeStartArray(JsonGenerator.java:770)
	at com.fasterxml.jackson.core.util.JsonGeneratorDelegate.writeStartArray(JsonGeneratorDelegate.java:206)
	at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:78)
	at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:18)
	at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727)
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:722)
	at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:166)
	at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:119)
	at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:79)
	at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:18)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
	at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1433)
	at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:921)
	at com.hubspot.jackson.jaxrs.PropertyFilteringMessageBodyWriter.writeValue(PropertyFilteringMessageBodyWriter.java:128)
	at com.hubspot.jackson.jaxrs.PropertyFilteringMessageBodyWriter.writeTo(PropertyFilteringMessageBodyWriter.java:72)
	at org.apache.cxf.jaxrs.utils.JAXRSUtils.writeMessageBody(JAXRSUtils.java:1472)

Avoid load of not requested lazy properties

On a JPA project, when a POJO has a List<Bean> property which should be lazy-loaded, the loading gets triggered even if I'm not requesting the property in the filter. It would be great to instruct Jackson not to call the getter of these properties to avoid unnecessary loading!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.