Search code examples
jax-rsjsonb

How to use JSON-B to serialize a transient property?


Given a

public class Foo {

    private String x;
    transient private String y;

    public String getX() {
        return x;
    }

    public void setX(String x) {
        this.x = x;
    }   

    public String getY() {
        return y;
    }

    public void setY(String y) {
        this.y = y;
    }   

}

How can you instruct JSON-B to serialise property y?

This is a case where type Foo is used in a JPA context (@Entity) where transient indicates a property should not be persisted.


Solution

  • Instead of using the transient keyword, annotate the field with the @javax.persistence.Transient annotation.

    i.e.

    
        @Transient
        private String y;
    
    

    Effectively this makes the property available to JSON-B for serialisation.