Search code examples
javaspringspring-mvcspring-bootspring-restcontroller

How do you force Spring MVC4 REST webservices to only accept parameters in json format?


Is there a way to force Spring MVC4 REST webservices to accept parameters or request in certain formats? I want my webservice to accept JSON parameters no by just appending a parameter to the url endpoint. I tried the example below but you can pass a parameter though the url i want them to pass it in a clean json format how do i do this in spring.

 @RequestMapping(value = "/user/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<User> getUser(@PathVariable("id") long id) {
    System.out.println("Fetching User with id " + id);
    User user = userService.findById(id);
    if (user == null) {
        System.out.println("User with id " + id + " not found");
        return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity<User>(user, HttpStatus.OK);
}

Solution

  •  @RequestMapping(...,consumes= MediaType.APPLICATION_JSON_VALUE)