Olá pessoal, então...tentando aplicar o que aprendi criando eu mesmo uma api, estou com dificuldades para entender o motivo de alguns campos estarem me retornando null, quando faço um post no insominia ele me dá um status 201 CREATED porém como já mencionei antes, com alguns campos nulos, mostrando é melhor que falando.
E agora vou mostrar a vocês meu DTO, Model e Controller.
DTO:
public record BooksRecordDTO(
@NotBlank
String title,
@NotBlank
String isbn,
@NotNull
Integer yearPublication,
@NotBlank
String cover,
@NotBlank
String description,
@NotNull
Integer pages,
@JsonProperty("author")
@Valid
AuthorRecordDTO author,
@JsonProperty("publishing_company")
@Valid
PublishingCompanyRecordDTO publishingCompany,
@JsonProperty("gender")
@Valid
GenderRecordDTO gender
) {
}
Model:
@Entity
@Table(name = "TB_BOOKS")
public class BooksModels extends RepresentationModel<BooksModels> implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
@ManyToOne
@JoinColumn(name = "author_id")
private AuthorModels author;
private String isbn;
private Integer yearPublication;
@ManyToOne
@JoinColumn(name = "publishing_company_id")
private PublishingCompanyModels publishingCompany;
private String cover;
@ManyToOne
@JoinColumn(name = "gender_id")
private GenderModels gender;
private String description;
private Integer pages;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public AuthorModels getAuthor() {
return author;
}
public void setAuthor(AuthorModels author) {
this.author = author;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public Integer getYearPublication() {
return yearPublication;
}
public void setYearPublication(Integer yearPublication) {
this.yearPublication = yearPublication;
}
public PublishingCompanyModels getPublishingCompany() {
return publishingCompany;
}
public void setPublishingCompany(PublishingCompanyModels publishingCompany) {
this.publishingCompany = publishingCompany;
}
public String getCover() {
return cover;
}
public void setCover(String cover) {
this.cover = cover;
}
public GenderModels getGender() {
return gender;
}
public void setGender(GenderModels gender) {
this.gender = gender;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getPages() {
return pages;
}
public void setPages(Integer pages) {
this.pages = pages;
}
}
Controller:
@RestController
public class BooksController {
@Autowired
BooksRepository booksRepository;
@PostMapping("/api/v1/books")
public ResponseEntity<BooksModels> saveBooks(
@RequestBody @Valid BooksRecordDTO booksRecordDTO) {
var booksModels = new BooksModels();
BeanUtils.copyProperties(booksRecordDTO, booksModels);
return ResponseEntity.status(HttpStatus.CREATED).body(booksRepository.save(booksModels));
}
}