0

I have the most basic ManyToMany example, Project and Employee.

With a PUT request, I will project to employee and within the same function employee to project.

public Employee assignProjectToEmployee(Long empId, Long projectId) {
...
projectSet.add(project);
employee.setAssignedProjects(projectSet);

part of code Employee Entity:

    @ManyToMany
    @JoinTable(name = "youtube-dev-2-prod-coding-employee_project",
            joinColumns = @JoinColumn(name = "employee_id"),
            inverseJoinColumns = @JoinColumn(name = "project_id"))
    @JsonManagedReference
    private Set<Project> assignedProjects = new HashSet<>();

part of code Project Entity:

    @JsonBackReference
    @ManyToMany(mappedBy = "assignedProjects")
    private Set<Employee> employeeSet = new HashSet<>();

✅When sending POST with Postman or Curl to save new Project, works fine.

🔴When sending POST with Postman or Curl to save new Employee, I get

"timestamp": "2025-08-28T17:12:19.998+00:00",
    "status": 415,
    "error": "Unsupported Media Type",

nota bene, when sending GET request for Employee, I would like to see all the Projects that employee is assigned on, and when sending GET request for Project, I would like to see all the Employees on that project. My question naturally is, why am I getting that 415 error

Code: https://github.com/beaverDamDemo/for-stackoverflow-question/tree/master (you would need to set the package in these files to make it work)

I tried examples from three different youtube tutorials and one example, taken from some published code on github

1 Answer 1

0
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/youtube-dev-2-prod-coding/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/save")
    public ResponseEntity<Employee> saveEmployee(@RequestBody Employee empObj) {
        employeeService.saveEmployee(empObj);
        return new ResponseEntity<>(HttpStatus.CREATED); // <-- focus at here.
    }

problem at https://github.com/beaverDamDemo/for-stackoverflow-question/blob/master/EmployeeController.java#L18

return new ResponseEntity<>(HttpStatus.CREATED);

best practice, but may be quite hard for new comer. employeeService.saveEmployee(empObj); it needs return created object. But if is hard for you, try to return just a simple Employee object in body, even dummy employee, it will removing the error.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/youtube-dev-2-prod-coding/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/save")
    public ResponseEntity<Employee> saveEmployee(@RequestBody Employee empObj) {
        employeeService.saveEmployee(empObj);
        Employee employee = new Empoyee();
        employee.set...
        employee.set...
        return new ResponseEntity<>(HttpStatus.CREATED).body(employee); // <-- focus at here.
    }

see this line

return ResponseEntity.created(location).header("MyResponseHeader", "MyValue").body("Hello World");

at https://docs.spring.io/spring-framework/docs/4.1.0.RC1_to_4.1.0.RC2/Spring%20Framework%204.1.0.RC2/org/springframework/http/ResponseEntity.html#:~:text=return%20ResponseEntity.created(location).header(%22MyResponseHeader%22%2C%20%22MyValue%22).body(%22Hello%20World%22)%3B

Sign up to request clarification or add additional context in comments.

2 Comments

Update 🔴It continues to throw 415 if I use this code: ``` @PostMapping("/save") public void saveEmployee(@RequestBody Employee empObj) { employeeService.saveEmployee(empObj); } ``` as well if I use this code: ``` @PostMapping("/save") public ResponseEntity<Employee> saveEmployee(@RequestBody Employee empObj) { employeeService.saveEmployee(empObj); Employee dummy = new Employee(); dummy.setEmpId(0L); dummy.setEmpName("Dummy Name"); return new ResponseEntity<>(dummy, HttpStatus.CREATED); } ```
If I remove both @JsonManagedReference, it manages to save the new employee. But it will crash afterwards when trying to assign a project to an employee.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.