Boa tarde! Quando vou realizar testes no postman no endpoint /api/auth, o postman devolve um 500 Internal Server Error!
E no console, recebo a seguinte Exception:
2021-10-31 17:17:42.368 ERROR 11692 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: class br.com.develfoodspringweb.develfoodspringweb.models.User cannot be cast to class br.com.develfoodspringweb.develfoodspringweb.models.Restaurant (br.com.develfoodspringweb.develfoodspringweb.models.User and br.com.develfoodspringweb.develfoodspringweb.models.Restaurant are in unnamed module of loader 'app')] with root cause
java.lang.ClassCastException: class br.com.develfoodspringweb.develfoodspringweb.models.User cannot be cast to class br.com.develfoodspringweb.develfoodspringweb.models.Restaurant (br.com.develfoodspringweb.develfoodspringweb.models.User and br.com.develfoodspringweb.develfoodspringweb.models.Restaurant are in unnamed module of loader 'app')
classe user
@Data
@Entity
@Table(name = "users")
@NoArgsConstructor
public class User implements UserDetails {
//atributos
@OneToMany(mappedBy = "user")
private List<UserRequest> userRequest;
public User(String name, String cpf, String login, String password, String email, String address, String phone) {
this.name = name;
this.cpf = cpf;
this.login = login;
this.password = password;
this.email = email;
this.address = address;
this.phone = phone;
}
public User(UserForm userForm){
this.name = userForm.getName();
this.cpf = userForm.getCpf();
this.login = userForm.getLogin();
this.email = userForm.getEmail();
this.address = userForm.getAddress();
this.phone = userForm.getPhone();
}
@ManyToMany
private List<Profile> userProfile = new ArrayList<>();
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return this.userProfile;
}
@Override
public String getUsername() {
return this.email;
}
@Override
public String getPassword() {
return this.password;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}
Classe Restaurant
@Entity
@Table(name = "restaurants")
@Data
@NoArgsConstructor
public class Restaurant implements UserDetails {
//atributos
@OneToMany(mappedBy = "restaurant")
private List<Plate> plate;
public Restaurant(String name, String cnpj, String login, String password, String email, String address, String phone) {
this.name = name;
this.cnpj = cnpj;
this.login = login;
this.password = password;
this.email = email;
this.address = address;
this.phone = phone;
}
public Restaurant(RestaurantForm restaurantForm) {
this.name = restaurantForm.getName();
this.cnpj = restaurantForm.getCnpj();
this.login = restaurantForm.getLogin();
this.email = restaurantForm.getEmail();
this.address = restaurantForm.getAddress();
this.phone = restaurantForm.getPhone();
}
@ManyToMany(fetch = FetchType.EAGER)
private List<Profile> restaurantProfile = new ArrayList<>();
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return this.restaurantProfile;
}
@Override
public String getUsername() {
return this.email;
}
@Override
public String getPassword() {
return this.password;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}