typeMismatch.java.util.List when trying to set a list
I was able to recreate your error case using a form validation. You are probably trying to pass a form data that is [5, 3]
for the tags
variable with type List<Long>
, but passing with brackets break that structure, the value ought to be 5, 3
...
So what I've done is;
Create a dummy controller using your input;
@Controller public class TestController { @PostMapping public ModelAndView test(@Validated @ModelAttribute final PrmBcClipInsert prmBcClipInsert, final BindingResult bindingResult) { final ModelAndView modelAndView = new ModelAndView(); System.out.println(prmBcClipInsert.getTags()); modelAndView.setViewName("test"); return modelAndView; } }
Pass the form with
tags=[5,3]
, and get the following error inBindingResult
;org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'prmBcClipInsert' on field 'tags': rejected value [[5, 3]]; codes [typeMismatch.prmBcClipInsert.tags,typeMismatch.tags,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [prmBcClipInsert.tags,tags]; arguments []; default message [tags]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'tags'; nested exception is java.lang.NumberFormatException: For input string: "[5,3]"]
Which is the identical error that you were getting... So I presume either you get this
PrmBcClipInsert
as a form input like in my example, or you are trying to do a similar binding in some other part of your code...Pass the form with
tags=5,3
, no error...
There can be a custom converter to support for passing said array input with brackets in binding logic with something like;
@Component
public class LongListConverter implements Converter<String, List<Long>> {
@Override
public List<Long> convert(String source) {
return Arrays.stream(StringUtils.strip(source, "[]").split(","))
.map(StringUtils::strip)
.map(Long::new)
.collect(Collectors.toList());
}
}
With this, both 5, 3
& [5, 3]
can be supplied as value of tags
variable.