, . . , , 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;