確實存在任何方式如何註冊JSON解串器使用傑克遜2.X和Spring框架,而不是與標註每@JsonDeserialize(using = IdentifiableDeserializer.class)
財產實現特定接口的所有類型的所有類型?如何註冊通用JsonDeserializer爲它實現給定的接口
通用解串器
public class IdentifiableDeserializer extends JsonDeserializer<IdentifiableEntity> implements ContextualDeserializer{
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property)
throws JsonMappingException {
// ...
}
@Override
public IdentifiableEntity deserialize(JsonParser jp, DeserializationContext ctxt)throws IOException, JsonProcessingException {
// ...
}
}
實體應反序列化
public class MyEntity implements IdentifiableEntity{
private Long id;
public getId(){
return id;
}
//...
}
的貢獻莫過於
@Service("myEntityService")
private MyEntityService implements IdentifiableService<MyEntity>{
MyEntity findById(Long id){
//...
}
}
我嘗試了以下方法,但它不起作用。
<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="deserializersByType">
<map key-type="java.lang.Class">
<entry key="xx.yy.IdentifiableEntity">
<bean class="xxx.yyy.IdentifiableDeserializer" />
</entry>
</map>
</property>
</bean>
</property>
</bean>
也許'Module'? – fge