//有一个参数的返回的是string类型的字符串,我想用int接收,提供给后面使用,需写好转换逻辑 static class StringDeserializer extends JsonDeserializer<Integer> { @Override public Integer deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonNode node = jp.getCodec().readTree(jp); String errorCode = node.asText(); return (errorCode == null || "".equals(errorCode)) ? 0 : 1; } }
在该字段上加注解
@JsonDeserialize(using = StringDeserializer.class)
private int errorCode;
如果是xml类型的数据(String转money)
static class MoneyAdapter extends XmlAdapter<String, Money> { /** * {@inheritDoc} * * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(java.lang.Object) */ @Override public Money unmarshal(String v) throws Exception { return new Money(v); } /** * {@inheritDoc} * * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(java.lang.Object) */ @Override public String marshal(Money v) throws Exception { return v.toString(); } }
在相应字段加注解,就行了
@XmlJavaTypeAdapter(MoneyAdapter.class)
@XmlElement(name = "remain_value")
private Money remainValue;
时间: 2024-11-05 21:55:10