I'm trying to set up a react front end for a project with a java/spring boot back end. Get requests were simple enough and I can make the POST request call but the values that are stored in the database are NULL. Quite new to react so would appreciate any help here
The react request, using axios
import React from 'react';
import axios from 'axios';
export default class PostRequest extends React.Component {
state = {
firstname: '',
lastname: '',
biography: '',
email: '',
}
handleChange = event => {
this.setState({ firstname: event.target.value });
this.setState({ lastname: event.target.value });
this.setState({ biography: event.target.value });
this.setState({ email: event.target.value });
}
handleSubmit = event => {
event.preventDefault();
const author = {
firstname: this.state.firstname,
lastname: this.state.lastname,
biography: this.state.biography,
email: this.state.email
};
axios.post(`http://localhost:8088/authorsrest`, { author })
.then(res => {
console.log(res);
console.log(res.data);
console.log("meh");
console.log(author);
})
}
render() {
return (
<div>
<form onSubmit={this.handleSubmit}>
<label>
First Name:
<input type="text" name="firstname" onChange={this.handleChange} />
</label>
<label>
Last Name:
<input type="text" name="lastname" onChange={this.handleChange} />
</label>
<label>
Last Name:
<input type="text" name="biography" onChange={this.handleChange} />
</label>
<label>
Last Name:
<input type="text" name="email" onChange={this.handleChange} />
</label>
<button type="submit">Add</button>
</form>
</div>
)
}
}
The Post request in my controller
@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public void createNewAuthor(@RequestBody @Validated Author author) throws AuthorAlreadyRegisteredException {
author = new Author();
authorService.create(author);
}
I've made all fields in the author class nullable, it's just a standard model class
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
@Entity
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "author_gen")
@SequenceGenerator(name = "author_gen", sequenceName = "AUTHOR_SEQ", allocationSize = 1)
private long authorId;
@Column( length = 80)
private String firstname;
@Column( length = 80)
private String lastname;
@Column(length = 250)
private String biography;
@Column( length=50)
private String email;
public Author(String firstname, String lastname, String biography, String email) {
super();
this.firstname = firstname;
this.lastname = lastname;
this.biography = biography;
this.email = email;
}
public Author() {
}
public String getFullName() {
return this.firstname + " " + this.lastname;
}
public long getAuthorId() {
return authorId;
}
public void setAuthorId(long authorId) {
this.authorId = authorId;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getBiography() {
return biography;
}
public void setBiography(String biography) {
this.biography = biography;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (authorId ^ (authorId >>> 32));
result = prime * result + ((biography == null) ? 0 : biography.hashCode());
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + ((firstname == null) ? 0 : firstname.hashCode());
result = prime * result + ((lastname == null) ? 0 : lastname.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Author other = (Author) obj;
if (authorId != other.authorId)
return false;
if (biography == null) {
if (other.biography != null)
return false;
} else if (!biography.equals(other.biography))
return false;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (firstname == null) {
if (other.firstname != null)
return false;
} else if (!firstname.equals(other.firstname))
return false;
if (lastname == null) {
if (other.lastname != null)
return false;
} else if (!lastname.equals(other.lastname))
return false;
return true;
}
@Override
public String toString() {
return "Author [authorId=" + authorId + ", firstname=" + firstname + ", lastname=" + lastname + ", biography="
+ biography + ", email=" + email + "]";
}
}
the post request goes to the database however the fields (other than the generated id) are null
the console shows that the author object has values
Can anyone advise?
updating to add a screenshot of the network tab, it shows status 201 created, the database shows that the row is added - but the values are NULL


