如何避免请求和响应@jsoninfotype中的类型字段

时间:2018-03-28 09:36:56

标签: json jackson

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.EXISTING_PROPERTY,visible = false, property = "type")
    public interface ItemId {

        String getItemValue();

        String getItemType();

    }

  //implemented class   
    public class ItemIdImpl implements ItemId{
    //class properties
    private final String itemvalue;
        private final String itemtype;

        //getters & setters below 

    }

    This interface is passed into method as post body 

    post body format :

[ { “项目值”: “inpound”, “项目类型”: “时尚”, “类型”: “com.demo.sand.model.ItemIdImpl”}, { “项目值”: “inpound”, “项目类型”: “时尚”, “类型”: “com.demo.sand.model.ItemIdImpl”} ]

//request passed into method body
    public ItemStatus getItems(@RequestBody List<ItemId> items)

    i am getting proper response.

    But i want to avoid sending the type in the request body ....
    like below :
//request body to be passed

    [
    {"itemvalue":"inpound","itemtype":"fashion"}, 
    {"itemvalue":"inpound","itemtype":"fashion"}   
    ]

    its not working in this way .what annotations i can use to avoid sending 
    type in request body?

1 个答案:

答案 0 :(得分:0)

如果您有简单的接口/实现关系,则不应使用@JsonTypeInfo,因为只有多态使用(多种可能的实现)才需要。{/ p>

相反,您可以使用注释来指定实现(带有@JsonDeserialize(as=Impl.class)的接口/抽象类),或者为抽象类型注册映射(请参阅SimpleModule方法addAbstractTypeMapping())。

相关问题