After two days, I'm stuck with the following...
If I specify the type in the controller, it works:
@RestController
public class FooController {
@RequestMapping(method = RequestMethod.POST)
protected ResponseModel bar(@Valid @RequestBody Foo foo) {
// here I have foo of type Foo
}
}
However, if I try to take advantage of generics, it doesn't work:
public class Base<U> {
@RequestMapping(method = RequestMethod.POST)
protected ResponseModel bar(@Valid @RequestBody U entity) {
// here I get java.util.LinkedHashMap instead of U
// which, in the FooController, should be Foo
}
}
@RestController
public class FooController extends Base<Foo> {
}
Looking at the logs, when it hits the controller with defined type I get:
Read [class com...Foo] as "application/json;charset=UTF-8" with [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@cee662]
While at the generic one:
Read [U] as "application/json;charset=UTF-8" with [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@cee662]
Q: What should I do to take advantage of the Generic Type, and to be able to cut the repeated code from the controllers?
Note: this is NOT a duplicate, since all the other questions are about List<T>
or from an issue on ~2.6.*
Addendum: Yuri-M-Dias suggested downgrading, which I did, so 2.5.5 works with a Generic Type, but 2.8.4 doesn't. (At least for now I have a bad and good revision to work with...)
Going further into the downgrade...
I found out that the revision 2.7.9 is a good one, I get Foo
out of U
, while on the 2.8.0 I get the LinkedHashMap<K,V>
... maybe I missed something from the 2.8.0 release ?