In the previous tutorial, we have seen how we have used Spring Boot to handle only one request (i.e. /HelloWorld).
i.e. We ask Spring Boot, what should be the response for /HelloWorld. Spring Boot gives us a response Hello Beautiful World.
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloWorldController { @RequestMapping("/HelloWorld") public String beautifulWorld(){ return "Hello Beautiful World"; } }
Now, let us see how Spring Boot can be used to handle multiple requests.
Say this time we have three requests,
So if you see the above code, to handle the request /HelloWorld, only one method beautifulWorld() and @RequestMapping("/HelloWorld") is used.
@RequestMapping("/HelloWorld") public String beautifulWorld(){ return "Hello Beautiful World"; }
In the similar way, to handle the second request /Hi, we need to use a second method.
Let us name the second method as hiAll(). It looks somewhat like the below.
@RequestMapping("/Hi") public String hiAll(){ return "Hi! How are you?"; }
Similarly, let us handle the third request /WhichCourseIsThis using the method course(),
@RequestMapping("/WhichCourseIsThis") public String course() { return "This is Spring Boot Course"; }
Now, let us put all the methods together,
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloWorldController { @RequestMapping("/HelloWorld") public String beautifulWorld(){ return "Hello Beautiful World"; } @RequestMapping("/Hi") public String hiAll(){ return "Hi! How are you?"; } @RequestMapping("/WhichCourseIsThis") public String course() { return "This is Spring Boot Course"; } }
Now let us run the above application,
Once the application has started, let us hit the request /WhichCourseIsThis(i.e. http://localhost:8080/WhichCourseIsThis) in a web browser.
And if you see the above response in the web browser, it is This is Spring Boot Course.
Even in this case, when Spring Boot finds the URL http://localhost:8080/WhichCourseIsThis, it searches if there is any Controller in its Application Context and finds @RestController.
So it assumes there must be a mapping using @RequestMapping which maps the request /WhichCourseIsThis.
And finds that the course() method annotated with @RequestMapping.
@RequestMapping("/WhichCourseIsThis") public String course() { return "This is Spring Boot Course"; }
And returns This is Spring Boot Course as response, which is displayed in the web browser.
Similarly, when we hit the request /Hi (i.e. http://localhost:8080/Hi) in a web browser.
We get the response Hi! How are you?.
Even in this case Spring Boot looks for @RequestMapping that Maps to the request /Hi.
And finds that hiAll() method is associated to it.
@RequestMapping("/Hi") public String hiAll(){ return "Hi! How are you?"; }
And we get the response Hi! How are you? as response in the web browser.