0

I have just started studying Springboot. Everything worked fine until I run into this problem. I've searched every StackOverFlow topic and internet overall and none resolved my problems. I tried to set Content-Type and Accepts the right way but it still didn't work.

UserController:

package com.example.carnet.api;

import com.example.carnet.model.User;
import com.example.carnet.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/carnet/user")
public class UserController {
    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }


    @GetMapping("/all")
    public List<User> getUsers() {
        return userService.getUsers();
    }

    @GetMapping(path = "/{email}")
    public User getUserByEmail(@PathVariable("email") String email) {
        return userService.getUserByEmail(email);
    }

    @GetMapping("/validate")
    public boolean validateUser(@RequestParam("email") String email, @RequestParam("password") String password) {
        return userService.validateUser(email, password);
    }

    @PostMapping("/add")
    public void addUser(@RequestBody User user) {
        userService.addUser(user);
    }

    @DeleteMapping(path = "/{id}")
    public void deleteUserById(@PathVariable("id") int id) {
        userService.deleteUserById(id);
    }

    @PutMapping
    public void updatePassword(@RequestBody User user) {
        userService.updatePassword(user);
    }
}

User Model:

package com.example.carnet.model;
import com.fasterxml.jackson.annotation.JsonManagedReference;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.sql.Date;
import java.util.List;

@Table(name = "users")
@Entity
public class User {
@Id
private int user_id;

private String email;

private String password;

private String name;

private String surname;

private Date birth_date;


@OneToMany(mappedBy = "user")
@JsonManagedReference
private List<Rental> rentals;

public User() {
}

public int getUser_id() {
    return user_id;
}

public void setUser_id(int user_id) {
    this.user_id = user_id;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getSurname() {
    return surname;
}

public void setSurname(String surname) {
    this.surname = surname;
}

public Date getBirth_date() {
    return birth_date;
}

public void setBirth_date(Date birth_date) {
    this.birth_date = birth_date;
}
    }

Error after doing POST request with Postman:

{
        "timestamp": "2020-05-06T19:01:16.498+0000",
        "status": 415,
        "error": "Unsupported Media Type",
        "message": "Content type 'application/json;charset=UTF-8' not supported",
        "path": "/carnet/user/add"
    }

SOLVED: Removing @JsonManagedReference from User Model solved the problem!

  • can you please show your model classes? Are you using @JsonManagedReference anywhere in your code flow? – GaneshSreeju May 06 '20 at 19:40
  • If you're setting the `Content-type` header in Postman, try putting a space between `application/json;` and `charset=UTF-8` – Phil May 06 '20 at 23:54
  • I can see the @JsonManagedReference there in rentals attribute, Can you please remove and try – GaneshSreeju May 07 '20 at 02:02
  • @GaneshSreeju removing JsonManagedReference worked! Could you provide some more context and / or explain why this was creating this problem? Should I remove it in all the classes? I also use JsonBackReference on other models. Thanks is advance! – Marian Gabriel Neacsu May 07 '20 at 09:04

1 Answers1

0

This problem is because of the @JsonManagedReference in your model class, Try interchanging @JsonBackReference and @JsonManagedReference. It should work. Just explore the documentation for more info.

This is one similar issue reported for the same case.

GaneshSreeju
  • 303
  • 1
  • 13