【问题标题】:Spring Boot Jpa 1.4.0 - No converter found capable of converting from type [java.util.HashMap<?, ?>] to type PojoSpring Boot Jpa 1.4.0 - 找不到能够从类型 [java.util.HashMap<?, ?>] 转换为 Pojo 类型的转换器
【发布时间】:2017-01-21 20:14:21
【问题描述】:

我们有一个使用 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 Boot 的 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;
    }
}

【问题讨论】:

  • 请出示您的CustomDataDto
  • 添加了我们期待的 Pojo。

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


【解决方案1】:

只需检查你的 pojo 类

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

【讨论】:

    猜你喜欢
    • 2019-03-06
    • 2019-03-25
    • 1970-01-01
    • 2019-03-24
    • 2014-09-19
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 2018-02-15
    相关资源
    最近更新 更多