1

I have seen similar answer here:

Spring MVC: Complex object as GET @RequestParam

Spring map GET request parameters to POJO automatically

I really can't find document of this because this auto mapping is not even done by any annotation. (it doesn't even need @RequestParam in fact)

1) so far I only see simple mapping, the object contain all primitive data, how about if my request is a complicated JSON object which contain several level of attributes (a object contain other objects)? Will the auto-mapping still work?

2) so far I only see Spring controller can take in one auto-map object, can I let it auto-map more than one object? For example:

public @ResponseBody List<MyObject> myAction(MyObject myObject,
MyObject2 myObject2) { ... }

Anyone know where is the document to describe how the mapping work behind the scene? Based on my second question, if Spring allow to do such thing, what if I have same attributes name in MyObject and MyObject2, how the mapping will do?

Community
  • 1
  • 1
Sam YC
  • 10,725
  • 19
  • 102
  • 158
  • 1. Yes 2. depends. There is a difference between marshaling JSON and mapping request parameters it is a HUGE difference. Basically all of this is explained in the Spring Reference Guide which I suggest you gave a read. – M. Deinum Mar 21 '17 at 14:27
  • I supposed `RequestMapping` should be documented well in http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html but I really can't find any clue. – Sam YC Mar 21 '17 at 14:33
  • I can't really understand this `There is a difference between marshaling JSON and mapping request parameters it is a HUGE difference` do you mind share a link? You can rely in answer below if it helps. thanks. – Sam YC Mar 21 '17 at 14:34
  • As I said there is a difference between binding (which relies on request parameters) and marshaling a request body. All of this, as mentioned earlier, is documented in the spring reference guide. – M. Deinum Mar 21 '17 at 15:41

1 Answers1

1

If you do things like this:

public @ResponseBody List<MyObject> myAction(@RequestBody MyObject myObject) { ... }

Of course you can only have one body in your http request.

As long as you have Jackson in your classpath (spring boot will add this automatically) your objects will be marshalled correctly.

If the JSON in your body is incorrect you will get a 400 (Invalid Request) returned.

Essex Boy
  • 7,565
  • 2
  • 21
  • 24