InstantiationException 类无法实例化

java.lang.InstantiationException: cn.iocoder.yudao.hospital.dal.dataobject.community.HCommunityResources


java.lang.实例化异常:cn.iocoder.yudao.hospital.dataobject.community。

 

 

@GetMapping("/page")
    @Operation(summary = "获得用户社区资源和支持服务记录分页")
    @PreAuthorize("@ss.hasPermission('hospital:community-resources:query')")
    public CommonResult<PageResult<HCommunityResources>> getCommunityResourcesPage(@Valid CommunityResourcesPageReqVO pageReqVO) {
        String nickname = getLoginUserNickname();

        PageResult<HCommunityResources> pageResult = hCommunityResourcesService.getCommunityResourcesPage(pageReqVO);

        List<HCommunityResources> list = pageResult.getList();
        for (HCommunityResources hCommunityResources : list) {
            hCommunityResources.setNickname(nickname);
        }
        for (HCommunityResources hCommunityResources : pageResult.getList()) {
            System.out.println("hCommunityResources = " + hCommunityResources);
        }

        //这一步代码出错
        return success(BeanUtils.toBean(pageResult, HCommunityResources.class));
    }

 

 

原因是HCommunityResources  添加了@AllArgsConstructor 一个包含全部参数的构造器,导致覆盖了无参构造器

package cn.iocoder.yudao.hospital.dal.dataobject.community;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.hospital.annotations.GeneratedValueUUID;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.time.LocalDateTime;

/**
 * 用户社区资源和支持服务记录表
 */
@Data
@TableName("h_community_resources")
@AllArgsConstructor
public class HCommunityResources extends BaseDO {
    private static final long serialVersionUID = 1L;

    @GeneratedValueUUID
    @Schema(description = "主键UUID")
    private String id;

    @Schema(description = "用户ID")
    private Long userId;

    @Schema(description = "记录时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime recordDate;

    @Schema(description = "社区资源和支持服务描述")
    private String resourceDetails;

    @TableField(select=false)
    private String nickname;

}

 

实例化异常原因:
  1. 类是抽象的:如果 HCommunityResources 类被声明为抽象类,那么它不能被直接实例化。
  2. 没有无参构造函数:如果 HCommunityResources 类没有无参构造函数,那么在尝试通过反射或其他方式创建实例时会失败。
  3. 类是接口:如果 HCommunityResources 被声明为接口,那么也不能被直接实例化。

 

posted @ 2024-11-04 10:53  123456~~~  阅读(1)  评论(0)    收藏  举报
© 2026 你的名字 | 博客园