Spring Boot Jpa 1.4.0 - 找不到能够从类型[java.util.HashMap <! - ?,? - >]转换为类型Pojo

时间:2016-09-14 05:56:29

标签: hibernate spring-mvc spring-boot jackson spring-data-jpa

我们有一个运行Spring Boot 1.3和Postgres数据库的应用程序。自定义UserType是在数据库中为Json类型定义的。遵循并实现UserType以使用ObjectMapper返回Pojo。它工作得非常好。 但是现在使用Spring boot 1.4,我们得到了这个例外。

自定义用户类型的代码段。

@Override
    public Object nullSafeGet(ResultSet resultSet, String[] names, SessionImplementor sessionImplementor, Object owner) throws HibernateException, SQLException {
        final String result = resultSet.getString(names[0]);
        if(result == null) {
            return null;
        }
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            Object response = objectMapper.readValue(result.getBytes("UTF-8"), returnedClass());
            return response;
        } catch (Exception e) {
            throw new RuntimeException("Failed to process json request:"+e.getMessage(), e);
        }
    }

我可以看到发现数据库的响应,而objectmapper也会转换它。返回时会抛出此错误。

org.springframework.core.convert.ConverterNotFoundException:找不到能够从类型[java.util.HashMap]转换为[com.company.component.entity.dto.CustomDataDto]类型的转换器

我们在Spring启动时在WebAppConfiguration中配置了MappingJackson2HttpMessageConverter。

CustomDataDto Pojo:

package com.company.component.entity.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import org.springframework.boot.jackson.JsonComponent;

import java.io.Serializable;
import java.util.List;

@JsonInclude(value = JsonInclude.Include.NON_EMPTY)
public class CustomDataDto implements Serializable {
    private static final long serialVersionUID = 4884047700260085799L;
    String id;
    List<MessagesDto> messages;
    List<CommentsDto> comments;
    List<MsgCmtMappingDto> msgCmtMapping;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public List<MessagesDto> getComments() {
        return comments;
    }

    public void setComments(List<MessagesDto> comments) {
        this.comments = comments;
    }

    public List<CommentsDto> getMessages() {
        return messages;
    }

    public void setMessages(List<CommentsDto> messages) {
        this.messages = messages;
    }

    public List<MsgCmtMappingDto> getMsgCmtMapping() {
        return msgCmtMapping;
    }

    public void setMsgCmtMapping(List<MsgCmtMappingDto> msgCmtMapping) {
        this.msgCmtMapping = msgCmtMapping;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof CustomDataDto)) return false;

        CustomDataDto that = (CustomDataDto) o;

        if (!id.equals(that.id)) return false;
        if (!messages.equals(that.messages)) return false;
        if (!comments.equals(that.comments)) return false;
        return msgCmtMapping.equals(that.msgCmtMapping);

    }

    @Override
    public int hashCode() {
        int result = id.hashCode();
        result = 31 * result + messages.hashCode();
        result = 31 * result + comments.hashCode();
        result = 31 * result + msgCmtMapping.hashCode();
        return result;
    }
}

1 个答案:

答案 0 :(得分:0)

只需检查您的pojo课

您正在尝试将字符串(来自数据库)显示为map(您的pojo类中的字段)