Problems sending multiple objects via POST and SPRING -MVC

I am developing REST services that should receive some information. In this case, two objects and an attribute.

This is javascript where I am testing a POST request

var user = { username: "admin", password: "admin" }; var userToSubscribe = { username: "newuser", password: "newpassword", email: " user@1and1.es " }; var openid = "myopenid"; $.ajax({ url: '/myportal/rest/subscribeUser.json', type: 'POST', dataType: 'json', contentType: 'application/json', mimeType: 'application/json', data: JSON.stringify({ user: user, userToSubscribe: userToSubscribe, openid: openid}) }); 

POST request:

  JSON openid "myopenid" user Object { username="admin", password="admin"} userToSubscribe Object { username="newuser", password="newpassword", email=" user@1and1.es "} Source {"user":{"username":"admin","password":"admin"},"userToSubscribe":{"username":"newuser","password":"newpassword","email":" user@1and1.es "},"openid":"myopenid"} 

And the controller that processes the POST:

  @RequestMapping(method=RequestMethod.POST, value="/subscribeUser.json") public @ResponseBody Message subscribeUser(@RequestBody("user") User user, @RequestBody("userToSubscribe") User userToSubscribe, @RequestParam String openid){ ... } 

And mistake

POST subscribeUser.json 400 Invalid localhost request: 8080 990 B [:: 1]: 8080

What am I doing wrong?

thanks

+6
source share
3 answers

The request body will contain all the JSON content. Therefore, when you want to match JSON, you use only one annotated RequestBody parameter. You will need to do something like this:

 public @ResponseBody Message subscribeUser(@RequestBody String str) ObjectMapper mapper = new ObjectMapper(); JsonNode node = mapper.readTree(str); 

And then use the convertValue method for mapper to get different objects from the string.

 JsonNode node = mapper.readTree(str); User theUser = mapper.convertValue(node.get("user"), User.class); 

Similarly for other objects

+11
source

You cannot use @ModelAttribute in a RESTful method that accepts JSON. I believe the correct method is to use @RequestBody, as done here . Most likely, you will have to wrap the objects in some wrapper class, but I could be wrong there, as I have never personally tried to pass multiple JSON objects into one request before.

However, I think it would be nice if you rethought your REST api by removing the JSON arguments and passing them instead as part of the URI path, if possible. I would suggest reading this blog post .

+2
source

You can create a java bean (POJO) containing all objects, such as.

 class JavaBean{ private User user; private UserTOSubscribe userToSubscribe; private Long openId; // getter and setter } 

and pass this bean to the web service. so the web service looks like.

 @RequestMapping(method=RequestMethod.POST, value="/subscribeUser.json") public @ResponseBody Message subscribeUser(@RequestBody JavaBean javaBean) { ... } 
0
source

Source: https://habr.com/ru/post/956928/


All Articles