是Spring引导的新手。我正在尝试创建一个将在manager下添加车辆的端点。我下面有两个实体Vehicle和Manager,以及相应的DTO。 DTO经理引用了VehicleDTO
VehicleDTO
<form action = "edit.php" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label for="title">Title of the Article</label>
<input type="text" class="form-control" id="title" name="title" value="<?php echo $title; ?>">
<br \>
<label for="bodyContent">Content</label>
<textarea class="form-control" rows="5" id="content" name="content"><?php echo $content; ?></textarea>
<br>
<div class="row">
<div class="col-md-4">
<label for="exampleFormControlFile1">Upload Image Only</label>
<input type="file" name="image" class="form-control-file" id="image">
<div class="col-md-12">
<?php
if(empty($images_)){
//nnothing to display
}else{
echo "<img src='uploaded_files/uploaded_files_articles_images/$images_' class='img-responsive img-rounded'>";
}
?>
</div>
</div>
<div class="col-md-4">
<label for="exampleFormControlFile1">Upload File</label>
<input type="file" name="files" class="form-control-file" id="files">
<div class="col-md-12">
<?php
if(empty($files_)){
//nothing to display
}else{
echo "<a href='uploaded_files/uploaded_files_articles/$files_' download>".$files_."</p>";
}
?>
</div>
</div>
<div class="col-md-4">
<input type="hidden" name="id" value=<?php echo $_GET['id']; ?>>
</div>
</div>
<br>
<button type="submit" name="btn-update" class="btn btn-primary">Update</button>
</div>
</form>
我的ManagerDTO在下面。这参考了Vehcile DTO
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.EngineType;
import com.Manufacturer;
@JsonDeserialize(builder = VehicleDTO.VehicleDTOBuilder.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class VehicleDTO {
private Long vehicleid;
@NotNull(message = "vehcilenumber can not be null!")
@Size(min = 2, max = 14)
private String vehcilenumber;
@NotNull(message = "Engine Type can not be null!")
private EngineType enginetype;
@Min(1)
@Max(5)
private Integer rating;
private Manufacturer manufacturer;
private VehicleDTO(Long id, String vehcilenumber, EngineType enginetype,Integer rating,Manufacturer manufacturer){
this.vehcilenumber=vehcilenumber;
this.enginetype=enginetype;
this.rating=rating;
this.vehicleid=id;
this.manufacturer=manufacturer;
}
public static VehicleDTOBuilder newBuilder()
{
return new VehicleDTOBuilder();
}
public Long getvehicleid() {
return vehicleid;
}
public String getvehcilenumber() {
return vehcilenumber;
}
public EngineType getEnginetype() {
return enginetype;
}
public Integer getRating() {
return rating;
}
public Manufacturer getManufacture() {
return manufacturer;
}
@JsonPOJOBuilder(buildMethodName = "createVehicleDTO", withPrefix = "set")
public static class VehicleDTOBuilder{
private Long vehicleid;
private String vehcilenumber;
private EngineType enginetype;
private Integer rating;
private Manufacturer manufacturer;
public VehicleDTOBuilder setvehicleid(Long id) {
this.vehicleid = id;
return this;
}
public VehicleDTOBuilder setvehcilenumber(String vehcilenumber) {
this.vehcilenumber = vehcilenumber;
return this;
}
public VehicleDTOBuilder setEnginetype(EngineType enginetype) {
this.enginetype = enginetype;
return this;
}
public VehicleDTOBuilder setRating(Integer rating) {
this.rating = rating;
return this;
}
public VehicleDTOBuilder setManufacturer(Manufacturer manufacturer) {
this.manufacturer = manufacturer;
return this;
}
public VehicleDTO createVehicleDTO()
{
return new VehicleDTO(vehicleid, vehcilenumber, enginetype,rating,manufacturer);
}
}
}
我的控制器在下面
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.VehicleDO;
import javax.validation.constraints.NotNull;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ManagerDTO {
@JsonIgnore
private Long id;
@NotNull(message = "Username can not be null!")
private String username;
@NotNull(message = "Password can not be null!")
private String password;
private VehicleDO vehicle;
private ManagerDTO() {
}
private ManagerDTO(Long id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
private ManagerDTO(Long id, String username, String password,VehicleDO vehicle) {
this.id = id;
this.username = username;
this.password = password;
this.vehicle = vehicle;
}
public static ManagerDTOBuilder newBuilder() {
return new ManagerDTOBuilder();
}
@JsonProperty
public Long getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public VehicleDO getvehicle() {
return vehicle;
}
public static class ManagerDTOBuilder {
private Long id;
private String username;
private String password;
private VehicleDO vehicle;
public ManagerDTOBuilder setId(Long id) {
this.id = id;
return this;
}
public ManagerDTOBuilder setvehicle(VehicleDO vehicle) {
this.vehicle = vehicle;
return this;
}
public ManagerDTOBuilder setUsername(String username) {
this.username = username;
return this;
}
public ManagerDTOBuilder setPassword(String password) {
this.password = password;
return this;
}
public ManagerDTO createManagerDTO() {
return new ManagerDTO(id, username, password,vehicle);
}
}
}
因此,当我的控制器被调用时,我可以看到用户名和密码已被填充,而未填充车辆。不知道这里缺少什么,以及杰克逊未填充我的Java对象的原因。