0

There are many posts on SO about a Spring @RestController not returning a JSON Object but instead a String. Many of these problems dealt with improper annotations on the RestController. I read them over and tried to apply the solutions where appropriate to my rest controller, but I am still returning a String to my JavaSript AJAX handler. The one thing that is different this time with this post from other SO posts regarding this issue is that my rest controller is receiving a file upload and returning a rest response. My rest controller looks like the following.

@RestController
@RequestMapping(value="/api/admin")
public class AdminImport {
 @AutoWired
 private SystemService systemService;

 @RequestMapping(value="/import", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
 public @ResponseBody Map<String, Object> importData(@RequestParam("file") MultipartFile file, HttpServletRequest req, HttpServletResponse resp) throws IOException {
  Boolean success = false;
  try {
   if(!file.isEmpty()) {
    systemService.importData(file.getInputStream());
    success = true;
   }
  } catch(Exception e) { }
  Map<String, Object> map = new HashMap<>();
  map.put("success",success);
  return map;
 }
}

My current workaround is to parse the returned String into a JSON Object using JSON.Parse.

myapi.upload = function(data, callBack) {
 var options = {
  url : "api/admin/import",
  data : data,
  processData : false,
  type : "POST",
  contentType : false,
  mimeType : "multipart/form-data",
  success : function(r) { callBack(JSON.Parse(r)); }
  error : function(r) { callBack({"success":false, "msg":"Unknown error"}); }
 }
 $.ajax(options);
}

Any ideas on what I'm doing wrong? Is this String response something specific to uploading a file?

I have very similar logic in a different controller, but instead of accepting a file upload, it accepts a @RequestBody and the AJAX call back actually receives a JSON Object (as opposed to a String). Or is this something about the client side code?

Any help is appreciated.

1
  • json by definition is a string...it's a string representation of a javascript array or object. If you are returning a non json string why are you parsing it as json in success callback? What errors are thrown? Inspect the actual request in browser dev tools network to check status, response body data sent etc for more clues Commented Aug 24, 2015 at 21:52

2 Answers 2

1

Try explicitly setting the dataType option in your .ajax options object to "json" including the double quotes.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, it was a client side thing then, because setting dataType : "json" got it working. It was never the server side code then, imo. Because I'm looking at how I handle my other AJAX calls, and I always set dataType : "json" (that option just wasn't set for that upload code).
If you read about the dataType here (api.jquery.com/jQuery.ajax) you'll see it tries to make an intelligent guess. Spring may be responding with a mime type header of text so JQuery does nothing with the response. I haven't used Spring MVC for a few months but you can investigate the headers by looking through docs.spring.io/spring/docs/current/spring-framework-reference/…
1

All you have to do , is annotating your req with @RequestBody:

   @RequestMapping(value="/import", method=RequestMethod.POST, 
    produces=MediaType.APPLICATION_JSON_VALUE)
   public @ResponseBody Map<String, Object> importData(@RequestParam("file") 
   MultipartFile file, @RequestBody req, HttpServletResponse resp) {

      //you code goes here
   }

and in jquery side and your the json parser

 success : function(r) { callBack(r); }

Comments

Your Answer

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.