I'm using Postman to send a POST request with some values(I put two strings, one is for username and the other is for mail), but all data gets saved to first column ("username").
What am I doing wrong?
This is what I've tried.
{
"username": "mm46676",
"mail": "mm46676@gmail.com"
}
I expected mm46676 is going to be saved in username column and mm46676@gmail.com to be saved in mail column.
I'm using H2 console to see how it was saved in db.
EmployeeController.java
import com.incompatibleTypes.plancation.plancationapp.model.Employee;
import com.incompatibleTypes.plancation.plancationapp.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping(path = "/employee")
public class EmployeeController {
private final EmployeeRepository employeeRepository;
public EmployeeController(EmployeeRepository employeeRepository) {
this.employeeRepository = employeeRepository;
}
@GetMapping("/all")
public @ResponseBody Iterable<Employee> showAllEmployees(){
return employeeRepository.findAll();
}
@PostMapping(path = "/add")
public @ResponseBody String addNewEmployee (@RequestBody String username, String mail){
Employee employee = new Employee();
employee.setUsername(username);
employee.setMail(mail);
employeeRepository.save(employee);
return "Employee Saved!";
}
}
Employee model(if it can help in any way)
package com.incompatibleTypes.plancation.plancationapp.model;
import jakarta.persistence.*;
import lombok.Data;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
@Data
@Entity
@Table(name = "employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
private UUID id;
private Role role;
@Column(name = "username")
private String username;
@Column(name = "mail")
private String mail;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "department_id")
private Department department;
@ManyToOne
@JoinColumn(name = "minimal_vacation_day_id")
private VacationDay minimalVacationDay;
@ManyToMany
@JoinTable(
name = "employee_bonusvacdays",
joinColumns = @JoinColumn(name = "bonus_vacation_day_id"),
inverseJoinColumns = @JoinColumn(name = "employee_id")
)
private Set<BonusVacationDay> bonusVacationDays = new HashSet<>();
private LocalDateTime dateEmployed;
}
The problem in your code is when passing the request body as a method parameter. With this code:
public @ResponseBody String addNewEmployee (@RequestBody String username, String mail)
the whole JSON body goes as a String to the variable username
. You should pass an object with fields username
and mail
instead. This can be the entity or - even better - a dedicated POJO aka as DTO. Something like this:
public @ResponseBody String addNewEmployee (@RequestBody EmployeeDTO data) {
Employee employee = new Employee();
employee.setUsername(data.getUsername());
employee.setMail(data.getMail());
employeeRepository.save(employee);
return "Employee Saved!";
}