Spring JPA Query getResultList将数据作为application / json返回

时间:2018-04-20 14:45:57

标签: spring-data-jpa

我有一个使用Spring JPA的Web应用程序,我有以下功能,可以在我的集成测试中正常工作

public List<PermissionQueryDTO> findByCompanyAndEventType(int companyId, int eventTypeId) {
    Query query = getEntityManager().createNativeQuery(FIND_BY_COMPANY_AND_EVENT_TYPE_QUERY, "PermissionQueryMapping");
    query.setParameter("companyId", companyId);
    query.setParameter("eventTypeId", eventTypeId);
    return query.getResultList();
}

在我的Web App中运行时,我收到此错误消息:

Could not locate appropriate constructor on class : a.b.c.PermissionQueryDTO] as "application/json" using [org.springframework.http.converter.StringHttpMessageConverter@346a9eea]

任何人都知道我为什么会收到此错误

1 个答案:

答案 0 :(得分:0)

问题是结果集映射没有指定类型。我不得不改变这个:

@SqlResultSetMapping(
    name="PermissionQueryMapping",
    classes={
        @ConstructorResult(
            targetClass=PermissionQueryDTO.class,
            columns={
                @ColumnResult(name="prop_id"),
                @ColumnResult(name="prop_description"s),
                @ColumnResult(name="prop_code"),
                @ColumnResult(name="prop_pt_id"),
                @ColumnResult(name="permissionId"),
                @ColumnResult(name="uplineStationPermissionLevel"),
                @ColumnResult(name="previousStationPermissionLevel"),
                @ColumnResult(name="eventStationPermissionLevel"),
                @ColumnResult(name="nextStationPermissionLevel",
                @ColumnResult(name="downlineStationPermissionLevel"),
            }
        )
    } 
)

对此:

@SqlResultSetMapping(
    name="PermissionQueryMapping",
    classes={
        @ConstructorResult(
            targetClass=PermissionQueryDTO.class,
            columns={
                @ColumnResult(name="prop_id", type=Integer.class),
                @ColumnResult(name="prop_description", type=String.class),
                @ColumnResult(name="prop_code", type=String.class),
                @ColumnResult(name="prop_pt_id", type=Integer.class),
                @ColumnResult(name="permissionId", type=Integer.class),
                @ColumnResult(name="uplineStationPermissionLevel", type=Integer.class),
                @ColumnResult(name="previousStationPermissionLevel", type=Integer.class),
                @ColumnResult(name="eventStationPermissionLevel", type=Integer.class),
                @ColumnResult(name="nextStationPermissionLevel", type=Integer.class),
                @ColumnResult(name="downlineStationPermissionLevel", type=Integer.class),
            }
        )
    } 
)