0

I am writing a rest service looking something like this:

@POST
@Consumes("application/json")
public void save(@QueryParam("firstName") String firstName, @QueryParam("lastName") String lastName, @QueryParam("email") String email) {
    User user = new User(firstName, lastName, email);
    db.createUser(user);
}

I would like to be able to have something like an object parameter:

@POST
@Consumes("application/json")
public void save(@ObjectParam User user) {
    db.createUser(user);
}

It would be nice not having to specify what parameters i am expecting or writing my own object parser.

3
  • Have you tried it? Did you get an error? Commented Aug 3, 2015 at 19:10
  • @ObjectParam doesn't exist, but i would like something like that. Right now i don't have a way to use an object as parameter Commented Aug 3, 2015 at 21:08
  • See @WestonJones answer - everything you include in the parameter list is seen as message body - annotations are only needed if you want to have further parameters which are present as path or query params. Commented Aug 4, 2015 at 6:29

2 Answers 2

1

This should be sufficient

@POST
@Consumes("application/json")
public void save(User user) {
    db.createUser(user);
}

It should map the object to JSON as long as you include a JSON object in your POST that matches.

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

Comments

0

In Spring REST, one may do the following:

@RequestMapping(value = "/", method = RequestMethod.POST)
public ResponseEntity<Car> update(@RequestBody Car car) {
    ...
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.