Jersey Rules for Parsing JSON / Jackson Subtype Deserialization

I get JSONs as follows:

@POST
@Path("log")
public Map<String, List<OperationResult>> log(Stats stats) {
  ..
}

JSON examples:

{
  "eventType": 1
  "params": {
    "field1" : 10
  }
}

{
  "eventType": 2
  "params": {
    "field2" : "ten"
  }
}

I have a class structure (they are generated by jsonschema2pojo, let it not matter):

interface Params;
class Params1 implements Params{ public int field1; }
class Params2 implements Params{ public String field2; }

class Stats {
  public int eventType;
  public Params params;
}

How can I get Jersey to parse JSON, so if eventType = 1, then stats.params becomes an instance of Params1 and another Params2?

+4
source share
1 answer

, . . , , json. , , :)

json:

artur@pandaadb:~/tmp/test$ cat 1.json 
{
  "eventType": "1",
  "params": {
    "field1" : 10
  }
}
artur@pandaadb:~/tmp/test$ cat 2.json 
{
  "eventType": "2",
  "params": {
    "field2" : "10"
  }
}

2 . , eventType t , . .

:

public class Stats {

    @JsonProperty
    int eventType;


    public Params params;

    @JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=JsonTypeInfo.As.EXTERNAL_PROPERTY, property="eventType")
    @JsonSubTypes({ @Type(value = Param1.class, name = "1"), @Type(value = Param2.class, name = "2") })
    public void setParams(Params params) {
        this.params = params;
    }
}

JsonTypeInfo, :

JsonTypeInfo.Id.NAME 

, "eventType"

JsonTypeInfo.As.EXTERNAL_PROPERTY 

, . , Params. setter .

property="eventType" 

jackson,

JsonSubTypes , 2: ​​

@Type(value = Param1.class, name = "1") 

Param1.class, eventType "1"

PAram2.class , "2"

json. . , , . TypeConverters, , json . , Google , .

:

public interface Params {

    public static class Param1 implements Params {
        @JsonProperty
        int field1;
    }

    public static class Param2 implements Params {

        @JsonProperty
        String field2;
    }

}

, , .

, , :

JsonTypeInfo.As.EXTERNAL_PROPERTY
JsonTypeInfo.As.EXISTING_PROPERTY

: D , . , , , , .

.

, , :

artur@pandaadb:~/tmp/test$ curl -XPOST  "localhost:8085/api/v2/test" -d @1.json -H "Accept: application/json" -H "Content-Type: application/json"
io.gomedia.resource.Params$Param1
artur@pandaadb:~/tmp/test$ 
artur@pandaadb:~/tmp/test$ curl -XPOST  "localhost:8085/api/v2/test" -d @2.json -H "Accept: application/json" -H "Content-Type: application/json"
io.gomedia.resource.Params$Param2

, . , json .

, :)

( №2: , , . , )

EDIT

, , json . , json String ( ). .

, , ( , serailisation), :

public class EventTypeConverter implements Converter<Integer, String>{

    @Override
    public String convert(Integer value) {
        return String.valueOf(value);
    }

    @Override
    public JavaType getInputType(TypeFactory typeFactory) {
        return SimpleType.construct(Integer.class);
    }

    @Override
    public JavaType getOutputType(TypeFactory typeFactory) {
        return SimpleType.construct(String.class);
    }

}

, :

@JsonProperty
@JsonDeserialize(converter=EventTypeConverter.class)
String eventType;
+2

Source: https://habr.com/ru/post/1648634/


All Articles