InvalidDefinitionException:无法构造`com.vehicle.datatransferobject.VehicleDTO`的实例

时间:2018-07-16 18:32:52

标签: rest spring-boot jackson spring-restcontroller

是SpringBoot的新手。试图建立一个休息端点。我试图将我的vehicleDTO传递给我的控制器。但是在到达我的控制器之前,有一个错误。

InvalidDefinitionException:无法构造com.vehicle.datatransferobject.VehicleDTO的实例(不存在创建者,如默认构造一样):无法从对象值反序列化(不存在基于委托或基于属性的创建者)

下面是我的车辆DTO

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.myvehicle.EngineType;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class VehicleDTO {

    @JsonIgnore
    private Long id;

    @NotNull(message = "vehiclenumber can not be null!")
    private String vehiclenumber;

    @Min(2)
    @NotNull(message = "Seat count can not be less than 2!")
    private Integer vehicleseatcount;

    @NotNull(message = "Engine Type can not be null!")
    private EngineType enginetype;

    @Max(5)
    private Integer vehiclerating;



    private VehicleDTO(Long id, String vehiclenumber, Integer vehicleseatcount, EngineType enginetype,Integer vehiclerating){
        this.vehiclenumber=vehiclenumber;
        this.vehicleseatcount=vehicleseatcount;
        this.enginetype=enginetype;
        this.vehiclerating=vehiclerating;
        this.id=id;
    }

    public static VehicleDTOBuilder newBuilder()
    {
        return new VehicleDTOBuilder();
    }

    @JsonProperty
    public Long getId() {
        return id;
    }


    public String getvehiclenumber() {
        return vehiclenumber;
    }


    public Integer getvehicleseatcount() {
        return vehicleseatcount;
    }


    public EngineType getEnginetype() {
        return enginetype;
    }



    public Integer getvehiclerating() {
        return vehiclerating;
    }




    public static class VehicleDTOBuilder{

        private Long id;
        private String vehiclenumber;
        private Integer vehicleseatcount;
        private EngineType enginetype;
        private Integer vehiclerating;

        public VehicleDTOBuilder setId(Long id) {
            this.id = id;
            return this;
        }
        public VehicleDTOBuilder setvehiclenumber(String vehiclenumber) {
            this.vehiclenumber = vehiclenumber;
            return this;
        }
        public VehicleDTOBuilder setvehicleseatcount(Integer vehicleseatcount) {
            this.vehicleseatcount = vehicleseatcount;
            return this;
        }
        public VehicleDTOBuilder setEnginetype(EngineType enginetype) {
            this.enginetype = enginetype;
            return this;
        }
        public VehicleDTOBuilder setvehiclerating(Integer vehiclerating) {
            this.vehiclerating = vehiclerating;
            return this;
        }


        public VehicleDTO createVehicleDTO()
        {
            return new VehicleDTO(id, vehiclenumber, vehicleseatcount, enginetype,vehiclerating);
        }

    }

}

我的DTO具有一个称为EngineType的枚举类型

public enum EngineType {
    ELECTRIC, DIESEL
}

我的控制器看起来像这样

@PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public VehicleDTO addvehicle(@Valid @RequestBody VehicleDTO vehicleDTO) 
    {
        VehicleDO vehicleDO = Mapper.VehicleDO(vehicleDTO);
        return Mapper.makeVehicleDTO(Service.addvehicle(vehicleDO));
    }

有人可以帮助我解决什么问题。不确定是什么杰克逊图书馆期望将我的请求正文转换为Java对象

4 个答案:

答案 0 :(得分:2)

此例外:

  

InvalidDefinitionException:无法构造的实例   com.vehicle.datatransferobject.VehicleDTO(没有创建者,例如默认   构造,存在):无法从对象值反序列化(没有委托-   或基于资源的创作者)

表示Jackson找不到实例化VehicleDTO的方法,该方法是默认构造函数(无arg构造函数)或JsonCreator
使用构建器模式时,将配置VehicleDTO类,使Jackson用VehicleDTO实例化VehicleDTOBuilder,例如:

@JsonDeserialize(builder = VehicleDTO.VehicleDTOBuilder.class)
public class VehicleDTO {
      ...         
}

并使用JsonPOJOBuilder注释构建器,如下所示:

@JsonPOJOBuilder(buildMethodName = "createVehicleDTO", withPrefix = "set")
public static class VehicleDTOBuilder{ 
   ...
}

根据javadoc,JsonPOJOBuilder为:

  

用于配置Builder类的详细信息:其实例是   用作反序列化POJO值的构建器,而不是POJO   使用构造函数或工厂方法实例化。注意这个   注释不用于定义什么是POJO的Builder类:   相反,这是由以下对象的JsonDeserialize.builder()属性确定的:   JsonDeserialize。

答案 1 :(得分:0)

在将Lombok的@Builder@Data注释一起用于POJO类(用于连接到API(用于消费或提供响应))时,我遇到了这个错误

我删除了@Builder注释,然后它可以正常工作

答案 2 :(得分:0)

就我而言:

InvalidDefinitionException: Cannot construct instance of com.vehicle.datatransferobject.VehicleDTO (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

针对上述异常,我只编写了 Default构造子,该实例实例化了类并解决了问题。

默认构造函数:

public VehicleDTO() {
        super();
        // TODO Auto-generated constructor stub
    }

答案 3 :(得分:0)

除了davidxxx的回答。我用过龙目岛。就我而言,它看起来像这样:

@Data
@JsonDeserialize(builder = SomeClass.SomeClassBuilder.class)
@Builder(builderClassName = "SomeClassBuilder")
public class SomeClass {
    // ...

    @JsonPOJOBuilder(withPrefix = "")
    public static class SomeClassBuilder {
    }
}