I have a problem with @GetMapping
in Spring boot.
It's about my @GetMapping
function that doesn't serialize my id
on this model while getting all data from database:
//User.java
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "username")
private String username;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "joined_date")
@CreatedDate
private Date joinedDate;
@Column(name = "password")
private String password;
@Column(name = "bio")
private String bio;
@Column(name = "email")
private String email;
}
I tried many ways but cannot solve at all. Even this question: Spring boot @ResponseBody doesn't serialize entity id
This is the image below:
I did it! Because I forgot to put the getter/setter
for the model. And there are more potentials here I wanna tell:
private int id
should be changed to private Integer id
as @Muhammad Vaqas
told meAnd there is the full form of the model:
package com.harrycoder.weebjournal.user;
import java.util.Date;
import javax.persistence.*;
import org.springframework.data.annotation.CreatedDate;
import com.fasterxml.jackson.annotation.*;
@Entity
@Table(name = "users")
@JsonIgnoreProperties(value = {"createdAt", "updatedAt"},
allowGetters = true)
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "username")
private String username;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "joined_date")
@CreatedDate
private Date joinedDate;
@Column(name = "password")
private String password;
@Column(name = "bio")
private String bio;
@Column(name = "email")
private String email;
public Integer getId() {
return id;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getBio() {
return bio;
}
public String getEmail() {
return email;
}
public Date getJoinedDate() {
return joinedDate;
}
}