Domanda

I am currently prototyping replacing the GWT-RPC based backend of our application to a REST based API using RestyGWT on the frontend and Spring MVC on the backend.

My issue occurs during the Java <-> JSON type conversions that both frameworks attempt to resolve automatically. All of our data objects use private fields, and many of the fields do not provide java bean style setter methods. By default, neither framework would inspect the private fields of a class and so this conversion fails.

For Spring MVC it was simple enough to fix this by adding an annotation to the data objects:

@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)

For RestyGWT I have not found a suitable fix. The only available workaround I have found is to use default access to all fields and constructors which is far from ideal. Does anybody have a solution that will allow RestyGWT to inspect the private fields of a Java object?

È stato utile?

Soluzione

Try using @JsonProperty and @JsonCreator (do not remeber if both are necessary) on your fields.

public abstract class Parent 
{    
    @JsonCreator
    public Parent(@JsonProperty("name") String name)
    {
        this.name = name;
    }


    public String getName()
    {
        return name;
    }

    private String name;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top