//有一個參數的返回的是string類型的字符串,我想用int接收,提供給後面使用,需寫好轉換邏輯
static class StringDeserializer extends JsonDeserializer<Integer> { @Override public Integer deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { return (jp.getText() == null || "".equals(jp.getText())) ? 0 : 1; } }
在該字段上加註解java
@JsonDeserialize(using = StringDeserializer.class)
private int errorCode;ide
若是是xml類型的數據(String轉money)spa
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(); } }
在相應字段加註解,就好了 code
@XmlJavaTypeAdapter(MoneyAdapter.class)
@XmlElement(name = "remain_value")
private Money remainValue;xml