1

As the title says....

I want to build a POJO with four field variables and at certain runtime events create an instance of this POJO with access to possibly maybe two or three of the fields.

public class Category implements Serializable {

    private String name;

    private String description;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

Lets say I create a new Category object but I only want to be able to have access to the name field during runtime. Is there a design pattern I can use to achieve this? I thought about the strategy pattern and looked at the builder but I am still confused if I can do this in java.

Basically the overall goal is to grab an object from a database and return it as a JSON response in jax rs. But sometimes I dont want a complete object returned but only lets say halve of the object to be accessible at during certain runtime events. My apologies if this seems like a dumb question but I know what I want to do but just don't know the best way.

Grim
  • 2,398
  • 4
  • 35
  • 54
  • With most ORM tools typically you bring in either the whole object or nothing at all. So are saying you want the access level of your setters to change during runtime? – Tim Biegeleisen Jul 20 '16 at 01:28
  • Okay, since you're talking about *JSON* you may be looking for JSON projections. I know that they're supported with Spring and Jackson, but I don't know much about JAX-RS. – chrylis -cautiouslyoptimistic- Jul 20 '16 at 01:31
  • Projections is exactly what I need....question is can I do it without using spring and with just jackson.... – Grim Jul 20 '16 at 04:53
  • Perhaps you can dynamically add mixins to your jackson containing @JsonIgnore for the certain fields. Or you just write a configurable serializer/deserializer. – Uwe Allner Jul 20 '16 at 09:32

2 Answers2

1

I have the same problem with you, and my project was used springmvc,and the json tool is jackson.With the problem solved, I just use @JsonIgnore.For more details,just read jackson-how-to-prevent-field-serialization

Community
  • 1
  • 1
Demon Coldmist
  • 2,560
  • 2
  • 13
  • 21
  • I need to be able to change how the object is serialized and deserialized at runtime. From my understanding @JsonIgnore will always prevent that field from being serializable. – Grim Jul 20 '16 at 12:46
1

So someone correct me if I am wrong or see a better option than this...with alot of objects this can be alot of extra code for serialization and deserialization...Jackson Provisions is what I need. I can use the annotation @JsonView(DummyClass.class) on the field variable. I will accept this a the best answer in a day or two unless someone else posts a better response.

  // View definitions:
  class Views {
            static class Public { }
            static class ExtendedPublic extends PublicView { }
            static class Internal extends ExtendedPublicView { }
  }

  public class Bean {
            // Name is public
            @JsonView(Views.Public.class) String name;
            // Address semi-public
            @JsonView(Views.ExtendPublic.class) Address address;
            // SSN only for internal usage
            @JsonView(Views.Internal.class) SocialSecNumber ssn;
  }

With such view definitions, serialization would be done like so:

  // short-cut:
  objectMapper.writeValueUsingView(out, beanInstance, ViewsPublic.class);

  // or fully exploded:
  objectMapper.getSerializationConfig().setSerializationView(Views.Public.class);
  // (note: can also pre-construct config object with 'mapper.copySerializationConfig'; reuse)
  objectMapper.writeValue(out, beanInstance); // will use active view set via Config

  // or, starting with 1.5, more convenient (ObjectWriter is reusable too)
  objectMapper.viewWriter(ViewsPublic.class).writeValue(out, beanInstance);

This information was pulled from http://wiki.fasterxml.com/JacksonJsonViews

with jackson 2.3, I can do this with JAX-RS

   public class Resource {

      @JsonView(Views.Public.class)
      @GET
      @Produces(MediaType.APPLICATION_JSON )
      public List<Object> getElements() {
        ...
        return someResultList;
      }
    }
Grim
  • 2,398
  • 4
  • 35
  • 54