1

I am working through the book 'Spring MVC, Beginners Guide'. In one section it suggests using Matrix Variables to pass in a high and low price. In their example the declaration specifies a List of Strings as the second parameter (see below).

@MatrixVariable(pathVar="price") Map<String, List<String>> priceParams

However, as there will only ever be one value for each key, I thought I would try the declaration :

@MatrixVariable(pathVar="price") Map<String, String> priceParams

When I try to access the 'value' associated with a specific key stored in the Map (using iterator.getKey() and iterator.getValue(), I continually get a run time ClassCastException error.

"Request processing failed; nested exception is java.lang.ClassCastException: java.util.LinkedList cannot be cast to java.lang.String"

I have tried different data types e.g. Integer, BigInteger, Double, Float and the error is always the same (apart from it not being a String)

when I examine the priceParams in debug, the data type shows as a LinkedMultiValueMap and the target map as a LinkedHashMap.

I am beginning to suspect that it is not possible to declare a Matrix Variable using Map<K,V> but only Map<K, List<V>>

Declaring it in the form Map<K, List<V>> works but you have to cycle through all the entries in the List, even though only one exists.

Any help would certainly be appreciated

Bond - Java Bond
  • 3,972
  • 6
  • 36
  • 59
mavisto
  • 31
  • 4
  • This is already solved in another post. See the solution here: https://stackoverflow.com/questions/30539783/spring-mvc-missing-matrix-variable – Mauricio Osorio Feb 19 '20 at 20:55

2 Answers2

0

From documents

If the method parameter type is Map and a matrix variable name is specified, then the matrix variable value is converted to a Map assuming an appropriate conversion strategy is available.

If the method parameter is Map<String, String> or MultiValueMap<String, String> and a variable name is not specified, then the map is populated with all matrix variable names and values.

Thus you need to update as below (i.e. remove the pathVar)

@MatrixVariable Map<String, String> priceParams

Let know in comments if you still face any issues

Also refer this article for more insight.

Community
  • 1
  • 1
Bond - Java Bond
  • 3,972
  • 6
  • 36
  • 59
  • This doesn't work either. I have tried using an iterator to get the values from the map and the error remains the same. The 'Values' in the map are not simple strings. Iterating over the map I can get the class, I can get the key but the code always fails when I try to get the 'value' from the map. The 'value' is represented as a linked list with a first and last entry. I can see this when I debug the code. The value within the getValue method is showing as a linked list. – mavisto May 20 '16 at 10:38
  • Please post the URI used – Bond - Java Bond May 20 '16 at 11:21
0

It would appear that there was an issue with the Spring base code.

https://jira.spring.io/browse/SPR-14294

Appears to have been fixed and will be in a future release.

mavisto
  • 31
  • 4