0

I have the following code:

@RequestMapping(value="/mobile/device", method = RequestMethod.PUT)
public ResponseEntity<Void> flagDevice (@RequestBody List<MobileDeviceData> devicedataList, @RequestHeader(value="special_code") String specialCode) {
// Implementation details
}

Each instance of MobileDeviceData that gets created needs to have a param field filled in with the RequestHeader special_code.

How would I go about doing this so that it is fully populated by the time the flagDevice method body gets called?

Thanks in advance.

DavidR
  • 6,622
  • 13
  • 56
  • 70

1 Answers1

0

This is non trivial.

An HttpMessageConverter is already provided that deserializes the JSON, that's the MappingJackson2HttpMessageConverter. It has access to request headers. You could extend that class to also use the headers for deserialization (this is extremely difficult to do generically, as opposed to only for MobileDeviceData).

You could use Spring AOP, intercept the method, retrieve the arguments, cast to the appropriate types, and assign the value yourself.

The solution I would go for is the simplest: do it yourself in the handler method. Loop the the List and use a corresponding setter to set the specialCode for each MobileDeviceData.

Another option is to define your own HandlerMethodArgumentResolver specifically for List<MobileDeviceData> parameters that need to be constructed from header vales.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724