POST方法出错。使用GET和DELETE方法工作。
org.springframework.http.converter.HttpMessageNotReadableException:JSON解析错误:由于输入结束而没有要映射的内容;嵌套异常是com.fasterxml.jackson.databind.exc.MismatchedInputException:由于输入结束而没有要映射的内容
public class ParameterDeserializer<T> extends JsonDeserializer<Parameter<T>> {
@Override
public Parameter<T> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
ObjectMapper objectMapper = (ObjectMapper) jsonParser.getCodec();
ObjectNode jsonNodes = objectMapper.readTree(jsonParser);
Class<? extends Parameter> parameterClass = null;
Map parameterObjectMap = objectMapper.convertValue(jsonNodes, Map.class);
Object value = parameterObjectMap.get("value");
if (value instanceof String) {
parameterClass = StringValue.class;
} else if (value instanceof Long || value instanceof Integer) {
parameterClass = LongValue.class;
} else if (value instanceof Boolean) {
parameterClass = BoolValue.class;
} else if (value == null) {
return null;
}
return objectMapper.readValue(jsonParser, parameterClass);
}
}
@Data
@Entity
@Table(name = "parameter")
@Inheritance(strategy = InheritanceType.JOINED)
@JsonDeserialize(using = ParameterDeserializer.class)
@DiscriminatorColumn(name = "parameter", discriminatorType = DiscriminatorType.STRING)
public abstract class Parameter<T> {
[...]
private T value;
public abstract T getValue();
public abstract void setValue(T value);
}
@Entity
@DiscriminatorValue(value = "string")
public class StringValue extends Parameter<String> {
@Column(name = "string_value")
private String stringValue;
@Override
public String getValue() {
return stringValue;
}
@Override
public void setValue(String stringValue) {
this.stringValue = stringValue;
}
}