- @GetMapping annotation can also be replaced with @RequestMapping(method=RequestMethod.GET,path=”/path”)
- Getters of all the properties of an object is required if that object is returned from the controller else a whitelable error is occurred.
What happens in the background when SpringBoot loads up?
DispatcherServletAutoConfiguration is matched when SpringBoot starts up
- What has happened is we added dependency of spring starter web.
- Spring starter web has a dependency on SpringMVC framework.
- Therefore we get DispatcherServlet class in our class path.
- Similarly there also exist an ErrorMvcAutoConfiguration which is matched when SpringBoot starts up.
- Now when ErrorMvcAutoConfiguration is found on the class path we need to configure a error page.
- Hence it adds basicErrorController, errorAttributes and DefaultErrorViewResolverConfiguration this will inshort call the whitelabel error page.
- Along with this other things that are autoconfigured along SpringBoot startup is HttpMessageConvertersAutoConfiguration which internally calls JacksonAutoConfiguration which does the object to json conversion in SpringBoot
- Now what happens when you hit any URL in the after the content path is any request after [/] is received at the DispatcherServlet first which is the FrontController.(Note we need to configure the root URL pattern from which DispatcherServlet will start handing the request)
- Now any text after [/] is considered as URI and this URI is matched with the available mapping(mapping of URL and the controller to be called) present with the DispatcherServlet(Front Controller) and it calls the mapped controller and the method which is suppose to be triggered.
- Now the controller in our case is a @ResetController and one of the important annotation present in the ResetController is @ResponseBody and @ResponseBody annotation converts the response into some other format possibly a json or xml response.
Example : Handling a simple get request and returning a json
App.java
package GradleDemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
Bean : Book.java
package GradleDemo;
public class Book {
int id;
String name;
String author;
public Book(int id, String name, String author) {
super();
this.id = id;
this.name = name;
this.author = author;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getAuthor() {
return author;
}
}
BooksController.java
package GradleDemo;
import java.util.Arrays;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class BooksController {
@GetMapping("/books")
public List<Book> getAllBooks(){
return Arrays.asList(new Book(332,"Hello","Tyson"));
}
}
build.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '2.0.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.7.RELEASE'
}
repositories {
jcenter()
}
dependencies {
implementation 'com.google.guava:guava:28.0-jre'
testImplementation 'junit:junit:4.12'
implementation 'org.springframework.boot:spring-boot-dependencies:2.0.5.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
components {
withModule('org.springframework:spring-beans') {
allVariants {
withDependencyConstraints {
it.findAll { it.name == 'snakeyaml' }.each { it.version { strictly '1.19' } }
}
}
}
}
}
bootJar {
mainClassName = 'GradleDemo.App'
}
task runJar{
dependsOn 'assemble'
dependsOn 'jar'
doLast{
javaexec {
main="-jar";
args = [
"build/libs/"+rootProject.name+".jar"
]
}
}
}
Output :
Now when you see the output here when we returned an array of bean from the controller a json is actually returned this happens because of spring-boot-starter-json jar
Handling a path variable in get method
In the same above example we added a method to display path variable example :
package GradleDemo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class BooksController {
@GetMapping("/books")
public Book getAllBooks(){
Book book = new Book(332,"Hello","Tyson");
return book;
}
@GetMapping("/Hello-Library/{name}")
public String helloBook(@PathVariable String name){
return "Hi "+name+" you are welcome to the library";
}
}
Output :