Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用JsonTypeInfo忽略名称空间_Java_Json_Jsonschema_Pojo - Fatal编程技术网

Java 如何使用JsonTypeInfo忽略名称空间

Java 如何使用JsonTypeInfo忽略名称空间,java,json,jsonschema,pojo,Java,Json,Jsonschema,Pojo,我正在使用一个遗留应用程序,需要解组以下有效负载 <InventoryItem><Vehicle xis:type="ns2:car"/></InventoryItem> 它按预期工作。但是你怎么能忽略名称空间呢?我不得不引入一个解析器来解决它 @JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, include = JsonTypeInfo.As.EXISTING_PROPERTY, property

我正在使用一个遗留应用程序,需要解组以下有效负载

<InventoryItem><Vehicle xis:type="ns2:car"/></InventoryItem>

它按预期工作。但是你怎么能忽略名称空间呢?

我不得不引入一个解析器来解决它

@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type", visible = true)
@JsonTypeIdResolver(VehicleTypeResolver.class)
public class Vehicle

public class VehicleTypeResolver extends TypeIdResolverBase {
    private JavaType superType;

    @Override
    public void init(JavaType baseType) {
        superType = baseType;
    }

    @Override
    public JsonTypeInfo.Id getMechanism() {
        return JsonTypeInfo.Id.NAME;
    }

    @Override
    public String idFromValue(Object obj) {
        return idFromValueAndType(obj, obj.getClass());
    }

    @Override
    public String idFromValueAndType(Object obj, Class<?> subType) {
        String typeId = null;
        String[] split = ((Vehicle)obj).getType().split(":");
        typeId = split[0] + ':' + subType.getSimpleName();
        return typeId;
    }

    @Override
    public JavaType typeFromId(DatabindContext context, String id) {
        Class<?> subType = null;
        String[] split = id.split(":");
        switch (split[1]) {
            case "car":
                subType = Car.class;
                break;
            case "truck":
                subType = Truck.class;
        }
        return context.constructSpecializedType(superType, subType);
    }
}
快速提示:车辆上的“type”属性就是诀窍,它可以很好地解组编组,但要使编组工作,我必须添加以下注释才能使其工作

@JacksonXmlProperty(localName = "type", isAttribute = true, namespace = "http://www.w3.org/2001/XMLSchema-instance")
    @JsonProperty("type")
    private String type; 
vehicle.json
{
  "type": "object",
  "id": "#Vehicle",
  "additionalProperties": false,
  "javaType": "Vehicle",
  "properties": {
    "type": {
      "type": "string"
    },
    "color": {
      "type": "string"
    }
  }
}
car.json
{
  "type": "object",
  "id": "#Car",
  "additionalProperties": false,
  "javaType": "Car",
  "extends": {
    "type": "object",
    "existingJavaType": "Vehicle"
  }
}
truck.json
{
  "type": "object",
  "id": "#Truck",
  "additionalProperties": false,
  "javaType": "Truck",
  "extends": {
    "type": "object",
    "existingJavaType": "Vehicle"
  }
}
@JacksonXmlProperty(localName = "type", isAttribute = true, namespace = "http://www.w3.org/2001/XMLSchema-instance")
    @JsonProperty("type")
    private String type;