When using Spring MVC you may have to convert an Object to a MultiValueMap<String,String>. For example when you want to perform a HTTP call with parameters. This tutorial will show how to Spring MVC Convert Object to MultiValueMap<String,String>.
Install Jackson
You need Jackson. So add the dependency to your Maven project.
1 2 3 4 5 6 7 8 9 |
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> |
Object to MultiValueMap<String,String>
We use Jackson objectMapper to convert the Java Bean to a Java Map and then to Spring MultiValueMap :
1 2 3 4 5 6 7 8 9 10 |
@Autowired private ObjectMapper objectMapper MultiValueMap<String, String> convert(Object obj) { MultiValueMap parameters = new LinkedMultiValueMap<String, String>(); Map<String, String> maps = objectMapper.convertValue(obj, new TypeReference<Map<String, String>>() {}); parameters.setAll(maps); return parameters; } |