We are doing a migration from Spring 5.0 to Spring Boot 2.4 version. My Controller code is as follows
@RequestMapping(value = "/getTreeNodesByFilter.action", method = RequestMethod.GET)
public @ResponseBody Map<String, ? extends Object> getTreeNodesByFilter(@RequestParam("type") String type,
@RequestParam("id") Long id, @RequestParam("pageNo") int pageNo, @RequestParam("pageSize") int pageSize,
@RequestParam("filterBy") String filterBy) {
Map<String, Object> responseMap = commonService.getTreeNodesByFilter(type, id, pageNo, pageSize, filterBy);
return ResponseUtil.getMap(responseMap);
}
The response body is a HashMap is seen above. This code works fine in normal spring webapp. The same code when tired with SpringBoot we are getting the following error
java.lang.ClassCastException: class java.util.HashMap cannot be cast to class com.scriptless.web.security.exceptions.JsonError (java.util.HashMap is in module java.base of loader 'bootstrap'; com.scriptless.web.security.exceptions.JsonError is in unnamed module of loader org.springframework.boot.devtools.restart.classloader.RestartClassLoader @5f00ac7a)
This error is thrown from AbstractMessageConverterMethodProcessor.class of Spring-webmvc.jar while executing the code
((HttpMessageConverter) converter).write(body, selectedMediaType, outputMessage);
Can someone please suggest what has to done to resolve this issue?
Try this objectMapper,
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
.configure(MapperFeature.USE_GETTERS_AS_SETTERS, false);
SimpleModule module = new SimpleModule();
objectMapper.registerModule(module);
objectMapper.registerModule(new JavaTimeModule());
PolymorphicTypeValidator ptv = BasicPolymorphicTypeValidator.builder()
.allowIfSubType("java.util.") //$NON-NLS-1$
.build();
objectMapper.setPolymorphicTypeValidator(ptv);
objectMapper.activateDefaultTyping(ptv, DefaultTyping.NON_FINAL);
return objectMapper;
}
This would allow all subtypes of package java.util.* to deserialize.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.