So I'm trying to build a website using Spring Boot and React.
So far I have a registration form that is connected to my spring backend at localhost:8080/api/test/customers (Have endpoints for CRUD operations here)
This woks fine, and I can post form data to it from my React form, and I can view it in postman to get the expected response from the API.
But now I have another form on the site (A contact form where a user enters their name, email and message).
I have this API at localhost:8080/api/form/contact for GET and POST requests.
I have mapped the Entities 'Customer' and 'Contact Form' using a 1:M relationship which contains the customer id as a FK in the Contact Form table.
The thing is, since I added this, The response from the API is not what I'm expecting.
The response goes on for like this for 200 lines in postman, just nesting the same stuff
The GET request to the Customer API (registration)
The GET request to the Contact Form API
My Entity Classes
Customer.java - Removed Validation Annotations to make it easier to read
@Entity
@Table(name="customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="customer_id")
private int customerId;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="email_address")
private String emailAddress;
@Column(name="date_of_birth")
private Date dateOfBirth;
@Column(name="mobile_number")
private String mobileNumber;
@Column(name="password")
private String password;
@OneToMany(mappedBy = "customer")
private Set<ContactForm> contactForm;
// default & arg constructor
// getters and setters
}
ContactForm.java - Removed Validation Annotations to make it easier to read
@Entity
@Table(name="contact_form")
public class ContactForm {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="contact_form_id")
private int id;
@Column(name="name")
private String name;
@Column(name="email_address")
private String emailAddress;
@Column(name="message")
private String message;
@ManyToOne
@JoinColumn(name = "customer_id")
private Customer customer;
// default and arg constructor
// getters & setters
}
MySQL DDL for these 2 tables
customer
CREATE TABLE `customer` (
`customer_id` int NOT NULL AUTO_INCREMENT,
`mobile_number` varchar(12) DEFAULT NULL,
`date_of_birth` date DEFAULT NULL,
`email_address` varchar(255) DEFAULT NULL,
`first_name` varchar(255) DEFAULT NULL,
`last_name` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
PRIMARY KEY (`customer_id`)
)
contact_form
CREATE TABLE `contact_form` (
`contact_form_id` int NOT NULL AUTO_INCREMENT,
`email_address` varchar(255) NOT NULL,
`message` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`customer_id` int DEFAULT NULL,
PRIMARY KEY (`contact_form_id`),
FOREIGN KEY (`customer_id`) REFERENCES `customer` (`customer_id`)
)
Controller class for customer
Controller Class with standard endpoints, same for ContactForm Controller
I cant post data to either endpoints anymore, Can someone please let me know why this is happening, I would like to have a record of the users and all their previous messages.