1

In my springboot project I have a module named

model

Here I have a class RealmRole:

public class RealmRole {

private String id;
private String name;

public RealmRole() {
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

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

@Override
public String toString() {
    return "RealmRole{" +
            "id='" + id + '\'' +
            ", name='" + name + '\'' +
            '}';
}

}

From a request I get a

List<RealmRole>

Now I want to filter a RealmRole by name:

realmRoleList.stream().filter(realmRole.getName().equals("user")).forEach(sout)

This causes the error:

Message: class java.util.LinkedHashMap cannot be cast to class de.test.userregistration.model.RealmRole (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; de.test.userregistration.model.RealmRole is in unnamed module of loader 'app')

Even if I just try:

realmRoleList.stream().forEach(sout) I get the same error.

I've alread read some questions but I could not find an answer for this issue. I already removed devtools from pom.xml because I've read that this could lead to classloader problems.

FishingIsLife
  • 1,972
  • 3
  • 28
  • 51
  • Did you mean you get an error when you use `realmRoleList.stream().filter(realmRole -> realmRole.getName().equals("user")).forEach(System.out::println);` ? – Youcef LAIDANI Nov 16 '20 at 09:50
  • Oh yes sorry, I updated the question – FishingIsLife Nov 16 '20 at 09:51
  • 1
    You *think* you have a `List` but in reality you don't. Generics can only ensure type safety at compile time but I guess at runtime the deserialization of your request does not work as you expect it and instead of deserializing your request into a `List` you get a `List` instead :-) – Smutje Nov 16 '20 at 09:54
  • But after the request I a valid List with entities. Now the error occurs even if I do just a simple for loop (for RealmRole realmRole: realmRoleList). It seems you are right but wth is the list looking ok – FishingIsLife Nov 16 '20 at 09:56
  • Can you add the code that receives the request? – Smutje Nov 16 '20 at 09:57
  • 1
    I solved it and @Smutje was right. It still confusing to me but the response returns a RealmRole [ ] instead of a List but it is deserialized in a list and passed in my code as such. – FishingIsLife Nov 16 '20 at 10:03
  • Sadly this is one of the many problems with Generics in Java, backwards compatibility and such... :-) – Smutje Nov 16 '20 at 10:07
  • I assume you're retrieving the list from a request with a RestTemplate. If so have a look at this [answer](https://stackoverflow.com/a/21158245/2380948) in a similar question. I ran into the same problem. – bvwidt Nov 25 '20 at 15:31
  • No I changed to the webClient because restTemplate won‘t be supported in the future. – FishingIsLife Nov 25 '20 at 19:05

1 Answers1

0

Use this 'collect(Collectors.toList())' at the end to convert the result back to a list.

Wesley Gomes
  • 119
  • 1
  • 3
  • I already tried this but it resolves in the same error. Therefor I've split up the stream to see if there is a problem at a certain stage – FishingIsLife Nov 16 '20 at 09:59