Private Fields Serialization with JSON-B and JAX-RS 2.1 / Java EE 8

JSON-B from Java EE 8 serializes public fields or properties as default. For private field serialization:

public class Workshop {

    private String name;
    private int duration;

    public Workshop(String name, int duration) { = name;
        this.duration = duration;


you will have to implement a PropertyVisibilityStrategy (checkout Java EE 8: Serializing POJOs with JSON-B):

    public class PrivateVisibilityStrategy implements PropertyVisibilityStrategy {
        public boolean isVisible(Field field) {
            return true;
        public boolean isVisible(Method method) {
            return false;

...pass your custom configuration to the JsonbBuilder and return the instance with a ContextResolver:

    import javax.json.bind.Jsonb;
    import javax.json.bind.JsonbBuilder;
    import javax.json.bind.JsonbConfig;
    public class JSONConfigurator implements ContextResolver {
        public Jsonb getContext(Class type) {
            JsonbConfig config = new JsonbConfig().
                    withPropertyVisibilityStrategy(new PrivateVisibilityStrategy());
            return JsonbBuilder.newBuilder().

Now your POJO's private fields are going to be used for JSON-mapping:

public class WorkshopsResource {

    public Workshop workshop() {
        return new Workshop("Java EE 8", 42);


Result: {"duration":42,"name":"Java EE 8"}

Tested with Payara 5.

See you at Java EE Workshops at MUC Airport, particularly at the Effective Java EE 8 workshop

NEW: Online Workhop Effective WebApps without Frameworks is also coming to: MUC Airport.

Airport MUC workshops: Web (SPA, PWAs, Offline, Desktop, Mobile) Applications Essentials and Effective Web Applications. No migrations. #usetheplatform

Podcast: and newsletter:

A book about rethinking Java EE Patterns


Shouldn't it have a type parameter `... implements ContextResolver<Jsonb>`?

Posted by Rüdiger on October 19, 2018 at 06:13 AM CEST #

I use wildfly 14

If I create this Provider it is not used to deserialize the Classes. What am I doing wrong? Any suggestions?

Posted by chris on October 23, 2018 at 02:15 PM CEST #

Thanx for the tip!
I'd like to ask if there is a way to obtain the same Jsonb instance that we create in the ContextResolver when we want to use it programmatically?
My use case is that JSON arrives at a JAX-RS resource as form data (application/x-www-form-urlencoded)
and I'd like to use the same, configured Jsonb instance instead of re-creating another one ad-hoc.

Posted by Vagelis on January 22, 2019 at 05:33 PM CET #

@chris, @adam

This is not mentioned in the jax-rs 2.x specifications. This is just a Payara-only-Feature and shouldn't have been tagged JavaEE8.

See this issue on openliberty:

Posted by Ben M. on February 11, 2019 at 09:08 AM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments