How to use @RestController in Spring 4?




Asked on March 22, 2015

Hi Friends,


In Spring 4 new feature,  a new annotation @RestController has been introduced.  How to use @RestController in Spring 4? Thanks






Replied on March 22, 2015
@RestController  is itself annotated by @Controller and @ResponseBody annotation.   @RestController is used for REST web services.  Now the web service method need not to use @ResponseBody, if our service class has been annotated with  @RestController. We can create our web service class as

@RestController
@RequestMapping("/data")
public class PersonController {
}

Within this class we can create our web service method as

@RequestMapping("/person")
public Person getPersonDetail(@RequestParam(value = "id",required = false,
                                                defaultValue = "0") Integer id) {
return person;
}

Find the URL for complete example of  @RestController in Spring 4.





Write Answer











©2024 concretepage.com | Privacy Policy | Contact Us