-
Notifications
You must be signed in to change notification settings - Fork 38.7k
Description
David Smith opened SPR-14294 and commented
I have been trying to get Matrix Variables to work when they are declared as
@MatrixVariable
(pathVar="price") Map<String, String> params, OR
@MatrixVariable
Map<String, String> params,
I am completely unable to get the 'value' from the map. My aim is to cycle through each map entry, validate the key and retrieve the value. There is only one value for each key which is why I was declaring the matrix variable as a Map<String, String>. I have got the code to work using the declaration Map<String, List<String>> because it is valid for me to loop through the List<String> and get the value from the list, but I am unable to retrieve the value from the map when declared without the List. I can get the key, but whatever way I have tried to get the value (iterator, for/next loop, while loop) it always gives me a ClassCastException because it cannot cast between a LinkedList and whatever data type I'm using; a string in this case.
The code I am using to retrieve the value is as follows
for(Map.Entry<String, String> e : params.entrySet()) {
for( Product product : listOfProducts) {
if("low".equals( e.getKey())) {
System.out.println(e.getValue() );
}
}
}
The code fails at the point where it is trying to call the getValue method. I have debugged the code and evaluated the 'value' from HashMap.class and the datatype of the value is always shown as a LinkedList.
Because the Map is declared as Map<String, String> the compiler will not allow me to iterate through a String. However, the ClassCastException error is always because it is not possible to cast between a LinkedList (the return data type from getValue() in HashMap.class) and a String (the map declaration).
I don't know if this is a bug or a problem with my code, but I have been unable to find a single example where a MatrixVariable has been declared as a Map<K,V>. All the examples I have found, including a number of text books always show the map declaration as a Map<K, List<V>>. This declaration does work because the declaration and the returned value will allow iteration though the LinkedList that is returned from the HashMap.class
Affects: 4.2.6
Reference URL: http://stackoverflow.com/questions/37325634/spring-matrixvariable-usage-for-single-value-per-key
Referenced from: commits c2625f9