membersound
membersound

Reputation: 86627

How to map REST parameters to complex object?

I want to create a REST service with spring that takes a bunch of parameters. I'd like these parameters to be mapped automatically into a complex transfer object, like:

@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public String content(@RequestParam RestDTO restDTO) {
    Sysout(restDTO); //always null
}

public class RestDTO {
    private boolean param;
    //getter+setter
}

But: when I execute a query like localhost:8080/myapp?param=true the restDTO param remains null.

What am I missing?

Upvotes: 0

Views: 1135

Answers (3)

Sergey Yamshchikov
Sergey Yamshchikov

Reputation: 1019

So, I see few problems (if it's not mistyping of course):

  1. localhost:8080/myapp&param=true "&" isn't correct, you have to use "?" to split params from URL like localhost:8080/myapp?param=true.
  2. I don't see mapping value in @RequestMapping(method = RequestMethod.GET) (But if you caught the request you've made correct configuration).

Upvotes: 0

membersound
membersound

Reputation: 86627

It turned out I have to omit the @RequestParam for complex objects:

@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public String content(RestDTO restDTO) {
    Sysout(restDTO);
}

Upvotes: 0

Predrag Maric
Predrag Maric

Reputation: 24403

Try with localhost:8080/myapp?param=true.

Probably a case where another pair of eyes sees the obvious :)

EDIT

Remove @RequestParam from method signature, works for me.

Upvotes: 1

Related Questions