I have WebDeviceInfo and IOSDeviceInfo classes that are subclasses of DeviceInfo. How can I create a single endpoint in a Spring @RestController that will accept either IOSDeviceInfo or WebDeviceInfo?
Attempt #1
I tried to map the same RequestMapping to two different methods, one that would get called if the RequestBody could be mapped to a WebDeviceInfo and the other that would get called if the RequestBody could be mapped to a IOSDeviceInfo.
@RequestMapping(value = "/register-device", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public void registerWebDevice(@RequestBody final WebDeviceInfo webDeviceInfo) {
//register web device
}
@RequestMapping(value = "/register-device", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public void registerIOSDevice(@RequestBody final IOSDeviceInfo iosDeviceInfo) {
//register ios device
}
But this does not work, the second RequestMapping does not get registered and the application fails to start up because Spring sees that /register-device with the same RequestMethod and MediaType is already mapped to another method.
Attempt #2
Next, I tried accepting the superclass as the RequestBody and then casting it to the appropriate subclass.
@RequestMapping(value = "/register-device", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public void registerDevice(@RequestBody final DeviceInfo deviceInfo) {
if (deviceInfo instanceof WebDeviceInfo) {
final WebDeviceInfo webDeviceInfo = (WebDeviceInfo) deviceInfo;
//register web device
} else if (deviceInfo instanceof IOSDeviceInfo) {
final IOSDeviceInfo iosDeviceInfo = (IOSDeviceInfo) deviceInfo;
//register ios device
} else {
logger.debug("Could not cast deviceInfo to WebDeviceInfo or IOSDeviceInfo");
}
}
This does not work either. I always get:
Could not cast deviceInfo to
WebDeviceInfoorIOSDeviceInfo
Attempt #3
Finally, I tried just casting to the correct subclass inside a try/catch.
@RequestMapping(value = "/register-device", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public void registerDevice(@RequestBody final DeviceInfo deviceInfo) {
try {
final WebDeviceInfo webDeviceInfo = (WebDeviceInfo) deviceInfo);
//register web device
} catch (final ClassCastException ex) {
try {
final IOSDeviceInfo iosDeviceInfo = (IOSDeviceInfo) deviceInfo);
//register ios device
} catch (final ClassCastException ex2) {
logger.debug("Could not cast deviceInfo to WebDeviceInfo or IOSDeviceInfo");
}
}
}
Again I get error:
Could not cast deviceInfo to
WebDeviceInfoorIOSDeviceInfo
Is there any way to accomplish this, or am I going to have to create two separate methods with two different RequestMappings?