Wednesday, May 23, 2018

Controllers in Spring Boot


How Controller works in Spring Boot?

For traditional spring mvc architecture refer here, We were annotating spring mvc Controller Class with @Controller as shown below
 

@Controller
public class ViewsController {

@RequestMapping("/view1")
public String showView1(){
 return "register";
}

In the above code as we know,when the request is made to ApplicationContext/view1 the resource register file will be returned.

In addition to that,Spring boot supports rest controller as shown below


@RestController
@Produces("application/json")
public class BaseController {

@RequestMapping("/list")
private List getList(){
 return Arrays.asList("ONE","TWO","THREE");
}
}

As mentioned above the Annotation @RestController identifies that it needs to produce the content that is mentioned, Here the method getList() returns json as mentioned with annotaion @Produces("application/json").

Download Source Code 
Share:

0 comments:

Post a Comment