小编Ped*_*ers的帖子

迫使Jackson反序列化为特定的原始类型

我使用Jackson 1.8.3将以下域对象序列化和反序列化为JSON

public class Node {
    private String key;
    private Object value;
    private List<Node> children = new ArrayList<Node>();
    /* getters and setters omitted for brevity */
}
Run Code Online (Sandbox Code Playgroud)

然后使用以下代码对对象进行序列化和反序列化

ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(destination, rootNode);
Run Code Online (Sandbox Code Playgroud)

然后用反序列化反序列化

mapper.readValue(destination, Node.class);
Run Code Online (Sandbox Code Playgroud)

对象的原始值是字符串,双打,长整数或布尔值.但是,在序列化和反序列化期间,Jackson将Long值(例如4)转换为Integers.

我如何"强制"杰克逊将数字非十进制值反序列化为Long而不是Integer?

java json jackson

14
推荐指数
2
解决办法
3万
查看次数

Spring 3 DI使用通用DAO接口

我正在尝试将@Autowired注释与我的泛型Dao接口一起使用,如下所示:

public interface DaoContainer<E extends DomainObject> {
    public int numberOfItems();
    // Other methods omitted for brevity 
}
Run Code Online (Sandbox Code Playgroud)

我以下列方式在Controller中使用此接口:

@Configurable
public class HelloWorld {
    @Autowired
    private DaoContainer<Notification> notificationContainer;

    @Autowired
    private DaoContainer<User> userContainer;

    // Implementation omitted for brevity
}
Run Code Online (Sandbox Code Playgroud)

我已使用以下配置配置了应用程序上下文

<context:spring-configured />
<context:component-scan base-package="com.organization.sample">
<context:exclude-filter expression="org.springframework.stereotype.Controller"
   type="annotation" />
</context:component-scan>
<tx:annotation-driven />
Run Code Online (Sandbox Code Playgroud)

这只是部分工作,因为Spring只创建并注入了我的DaoContainer的一个实例,即DaoContainer.换句话说,如果我问userContainer.numberOfItems(); 我得到了notificationContainer.numberOfItems()的数量

我试图使用强类型接口来标记正确的实现,如下所示:

public interface NotificationContainer extends DaoContainer<Notification> { }
public interface UserContainer extends DaoContainer<User> { }
Run Code Online (Sandbox Code Playgroud)

然后像这样使用这些接口:

@Configurable
public class HelloWorld {
    @Autowired
    private NotificationContainer notificationContainer;
    @Autowired
    private UserContainer userContainer; …
Run Code Online (Sandbox Code Playgroud)

spring annotations dependency-injection interface autowired

7
推荐指数
1
解决办法
1万
查看次数