3

I have created a REST service using spring boot. Initially I had multiple parameters (Strings and Ints), but it was suggested that I send the request in json and automatically convert it to an object. But when I call the following REST service I get the following error:

Failed to convert value of type 'java.lang.String' to required type 'x.x.x.MobileCreatives'

@RequestMapping(method = RequestMethod.POST, value = "/creative")
 public @ResponseBody void uploadCreative(@RequestParam("mobileCreatives") MobileCreatives mobileCreatives,
 @RequestParam("file") MultipartFile file){
 logger.trace("Sending creative to DFP");
 mobileCreatives.setFile(file);
 dfpAssetsManager.createCreative(mobileCreatives);
 }

I would like to know why it is thinking that the input is a string, when the input is the following JSON:

{"networkCode":6437988,"iO":"test345345","name":"test4354","advertiserId":11659988,"clickThroughUrl":"test.com"}

My class MobileCreative has a constructor that is in the same format as the json. Do I need to add any annotations to the class MobileCreative becasue an example I was looking at didn't have any?

asked Apr 7, 2015 at 0:06
1
  • try RequestBody MobileCreative movileCreative Commented Apr 7, 2015 at 7:31

1 Answer 1

4

You want @RequestPart not @RequestParam. Per the documentation ...

The main difference is that when the method argument is not a String, @RequestParam relies on type conversion via a registered Converter or PropertyEditor while @RequestPart relies on HttpMessageConverters taking into consideration the 'Content-Type' header of the request part. @RequestParam is likely to be used with name-value form fields while @RequestPart is likely to be used with parts containing more complex content (e.g. JSON, XML)

answered Apr 7, 2015 at 7:17
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.