在spring boot中使用dto传递url源

bnl4lu3b  于 2021-07-16  发布在  Java
关注(0)|答案(0)|浏览(206)

我在dto里有dto。如何在post请求中传递资源url?我想传递已经在数据库中的drawervent,但不想在drawervent中创建另一个条目。

{
    "serialNumber": "1",
    "firstNumber": "1",
    "secondNumber": "1",
    "thirdNumber": "1",
    "fourthNumber": "1",
    "fifthNumber": "1",
    "sixthNumber": "1",
    "bonusNumber": "1",
    "drawEvent": "/drawEvents/1"
}

这会导致json解析错误。
这是结果实体

@Entity(name = "results")
@Data
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Result extends AbstractModel{

    @NotNull(message = "First Number can't be Empty")
    @Min(value = 1, message = "First number should be greater than or equal to 1")
    @Max(value = 45,message = "First number should be less than or equal to 45")
    private Integer firstNumber;

    @NotNull(message = "Second Number can't be Empty")
    @Min(value = 1, message = "Second number should be greater than or equal to 1")
    @Max(value = 45,message = "Second number should be less than or equal to 45")
    private Integer secondNumber;

    @NotNull(message = "Third Number can't be Empty")
    @Min(value = 1, message = "Third number should be greater than or equal to 1")
    @Max(value = 45,message = "Third number should be less than or equal to 45")
    private int thirdNumber;

    @NotNull(message = "Fourth Number can't be Empty")
    @Min(value = 1, message = "Fourth number should be greater than or equal to 1")
    @Max(value = 45,message = "Fourth number should be less than or equal to 45")
    private int fourthNumber;

    @NotNull(message = "Fifth Number can't be Empty")
    @Min(value = 1, message = "Fifth number should be greater than or equal to 1")
    @Max(value = 45,message = "Fifth number should be less than or equal to 45")
    private int fifthNumber;

    @NotNull(message = "Sixth Number can't be Empty")
    @Min(value = 1, message = "Sixth number should be greater than or equal to 1")
    @Max(value = 45,message = "Sixth number should be less than or equal to 45")
    private int sixthNumber;

    @NotNull(message = "Bonus Number can't be Empty")
    @Min(value = 1, message = "Bonus number should be greater than or equal to 1")
    @Max(value = 45,message = "Bonus number should be less than or equal to 45")
    private int bonusNumber;

    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(nullable = false)
    DrawEvent drawEvent;
}

这是票证实体

@Data
@Entity(name = "tickets")
public class Ticket extends Result {

    @GeneratedValue(generator = "UUID", strategy = GenerationType.AUTO)
    @GenericGenerator(
            name = "UUID",
            strategy = "org.hibernate.id.UUIDGenerator"
    )
    @PrePersist
    protected void onCreate() {
        setSerialNumber(UUID.randomUUID());
    }

    private UUID serialNumber;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "draw_event_id")
    DrawEvent drawEvent;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "user_id")
    User user;
}

这是dto的票

@Getter
@Setter
public class TicketDto extends AbstractModel {
    private Integer firstNumber;
    private Integer secondNumber;
    private int thirdNumber;
    private int fourthNumber;
    private int fifthNumber;
    private int sixthNumber;
    private int bonusNumber;
    DrawEventDto drawEvent;
    UserDto user;
}

这是事件

@Getter
@Setter
public class DrawEventDto extends AbstractModel {
    private String title;
    private String imageUrl;
    LocalDateTime drawDate;
    private Integer noOfTicket;
    private BigDecimal estimatedPrize;
}

我是ticketcontroller。

@PostMapping
    public ResponseEntity<ResponseMessage<?>> save(@RequestBody TicketDto ticketDto) {
        ResponseMessage responseMessage = ticketService.save(ticketDto);
        return new ResponseEntity<ResponseMessage<?>>(responseMessage, HttpStatus.CREATED);
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题