Issue
I have a REST API setup using Spring Boot. I'm attempting to send a JSON object in a POST request to the API to read the values and store them in a SQL Server database table, but it doesn't read all of the values.
This is the JSON object I'm sending:
{ "filename":"recording1POST", "creationdate":"13.11.2023", "userid":1, "relativefilepath":"filepathPOST", "camerasid":1 }
The is the class I'm mapping it to:
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
//Entity annotation defines class as being mapped to a table.
@Entity
@Table(name="Files")
public class Recording {
@Id//Marks the primary key of the table.
@GeneratedValue(strategy = GenerationType.IDENTITY)//Primary key auto-increments.
private int fileid;
@Column(name = "filename")
private String filename;
@Column(name = "creationdate")
private String creationdate;
@Column(name = "userid")
private int userid;
@Column(name = "relativefilepath")
private String relativefilepath;
@Column(name = "camerasid")
private int camerasid;
public int getFileID() {
return fileid;
}
public void setFileID(int fileID) {
this.fileid = fileID;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public String getCreationDate() {
return creationdate;
}
public void setCreationDate(String creationDate) {
this.creationdate = creationDate;
}
public int getUserID() {
return userid;
}
public void setUserID(int userID) {
this.userid = userID;
}
public String getRelativeFilepath() {
return relativefilepath;
}
public void setRelativeFilepath(String relativeFilepath) {
this.relativefilepath = relativeFilepath;
}
public int getCamerasID() {
return camerasid;
}
public void setCamerasID(int camerasID) {
this.camerasid = camerasID;
}
}
I'm getting the following result:
Spring Boot Server REST API not reading values from JSON
I haven't been able to find any questions with my specific problem. What might be causing this?
Edit: These are my database tables:
CREATE TABLE Users (
userid int IDENTITY(1,1) PRIMARY KEY,
username varchar(255),
password varchar(255)
);
CREATE TABLE Cameras (
cameraid int IDENTITY(1,1) PRIMARY KEY,
cameraname varchar(255),
camusername varchar(255),
campassword varchar(255),
userid int FOREIGN KEY REFERENCES Users(userid)
);
CREATE TABLE Files (
fileid int IDENTITY(1,1) PRIMARY KEY,
filename varchar(255),
creationdate datetime DEFAULT(getdate()),
userid int FOREIGN KEY REFERENCES Users(userid),
relativefilepath varchar(255),
camerasid int FOREIGN KEY REFERENCES Cameras(cameraid)
);
Solution
change setter/getter as below
public int getFileid() {
return fileid;
}
public void setFileid(int fileid) {
this.fileid = fileid;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public String getCreationdate() {
return creationdate;
}
public void setCreationdate(String creationdate) {
this.creationdate = creationdate;
}
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getRelativefilepath() {
return relativefilepath;
}
public void setRelativefilepath(String relativefilepath) {
this.relativefilepath = relativefilepath;
}
public int getCamerasid() {
return camerasid;
}
public void setCamerasid(int camerasid) {
this.camerasid = camerasid;
}
Answered By - DingHao
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.