Home > Back-end >  415 unsupported media type problem in the Entity class
415 unsupported media type problem in the Entity class

Time:01-17

I send a POST request to create a new user but I get an error: "415 unsupported media type" I have been reading the documentation and other questions on this topic and by now I have already checked the headers "Content-Type" and other. I also know for sure that it's not about dependencies and not about configuration. I created a simple TestClass and tested my code on it. Everything works perfectly. From this I conclude that the matter is in my Entity class, but I do not understand what exactly. This is my rest controller:

@RestController
@RequestMapping("/api/users")
@CrossOrigin(origins = "http://localhost:3000/", maxAge = 3600)
public class UserRestController {
    ...

    @PostMapping()
    public ResponseEntity<UserDto> create(@RequestBody User user) {
        User result = userService.create(user);
        return new ResponseEntity<>(UserDto.fromUser(result), HttpStatus.OK);
    }
}

My EntityClass:

@Entity
@Table(name = "users")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User extends BaseEntity {

    @Column(name = "username")
    private String username;

    @Column(name = "password")
    private String password;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @Enumerated(EnumType.STRING)
    @Column(name = "role")
    private Role role;

    @Column(name = "active")
    private boolean active;

    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JsonManagedReference
    private List<Restaurant> restaurants;

    @OneToMany(mappedBy = "author", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JsonManagedReference
    private List<Comment> comments;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "vote_id")
    @JsonBackReference
    private Vote vote;

    @Override
    public String toString() {
        return "User{"  
                "id='"   getId()   '\''  
                ", username='"   username   '\''  
                ", password='"   password   '\''  
                ", firstName='"   firstName   '\''  
                ", lastName='"   lastName   '\''  
                ", role="   role  
                ", active="   active  
                '}';
    }
}

And my BaseEntity class:

@MappedSuperclass
@Data
public class BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;

    @CreatedDate
    @Column(name = "created")
    private LocalDateTime created;

    @LastModifiedDate
    @Column(name = "updated")
    private LocalDateTime updated;

}

Here is what I get in Postman:

enter image description here

enter image description here

CodePudding user response:

It turned out that the problem is in the fields that are other objects of my application. The parser didn't know what to do with them. It was enough to ignore them (@JsonIgnore) and everything worked as it should.

@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JsonManagedReference
@JsonIgnore
private List<Restaurant> restaurants;

@OneToMany(mappedBy = "author", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JsonManagedReference
@JsonIgnore
private List<Comment> comments;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "vote_id")
@JsonBackReference
@JsonIgnore
private Vote vote;
  • Related