Tuesday, May 17, 2016

Spring boot quick overview


If there is a word they care about at Pivotal, it is production. Deploying apps and services to production effectively is what companies strive for, sometimes without finding the perfect formula. Spring Boot is meant to overcome this challenge, for Spring framework users. Imagine you have a genie that grants your software developement wishes before you even know them,  or, using a more serious definition, a framework that helps you get rid of all the boiler plate code and configuration, and focus on things that matter. It uses your build system (Maven or Gradle) to predict the things that you can possibly do with your application, and auto configure them for you. For example, if you include a Spring Web dependency, Spring Boot gently includes an embedded Tomcat server with an 8080 default port in your jar, no need to deploy, it's already done for you. Another wonder of Spring boot is database configuration. Once you include a database driver, it auto configures the data access layer for you. It also picks up any schemas or data files (.sql) and executes them on start up. Impressive, ha? This is only a drop in the bucket of what Boot could do.
 Spring Boot has some downsides as well. Additional effort is required by the developper to modify the default configuration if  needed, which is not always easy. In any case, let's give it a try.

In this tutorial, we will go through an example of how to quickly build a Rest application with an embeded database using Spring Boot.

We will start first by using SPRING INITIALIZR to quicky bootstrap our app.






There is a bunch of options to choose from. In our case, we chose: Web, JPA, and H2 database dependencies. We can now click on generate to download the project, and import it into an IDE ( eclipse for this tutorial).

We will then take care of the data access by creating our model object and the corresponding data access object (dao) to perform operations on the database.

 @Entity(name="users")
public class User {
 
 @Id @GeneratedValue
 int id;
 String userName;
 String firstName;
 String lastName;
 
 
 public User(){
  
  
 }

//.. getters & setters

}

Using Spring data, there is no need to programtically implement the operations, all we need to do is to extend CrudRepository.

public interface UserDao extends CrudRepository{
      public User findByUserName(String userName);
}
Notice that we have added a findByUserName method, which is also automatically implemented by Spring Data (based on the name of the method). For those, not familiar with Spring data this what this framework is about.

Next we can start implementing our Rest endpoints:



@Controller
public class UserController {
 
 @Autowired UserDao userdao;
 
 @RequestMapping(value="/users", method=RequestMethod.GET)
 public @ResponseBody List<User> getAllUsers(){
  return (List<User>) userdao.findAll();
 }
 
 @RequestMapping(value="/users", method=RequestMethod.POST)
 public ResponseEntity<Void> addUser(@RequestBody User newUser){
     if(newUser != null){
       if( userdao.save(newUser) == null){
         return new ResponseEntity<Void>(HttpStatus.SERVICE_UNAVAILABLE);
       }else{
        return new ResponseEntity<Void>(HttpStatus.CREATED);
       }
     }else{
      return new ResponseEntity<Void>(HttpStatus.CONFLICT);
     }
 }
 
 @RequestMapping(value="/users/{userName}", method=RequestMethod.DELETE)
 public ResponseEntity<Void> deleteUser(@PathVariable String userName){
  User searchedUser = userdao.findByUserName(userName);
  if(searchedUser != null){
   userdao.delete(searchedUser);
   return new ResponseEntity<Void>(HttpStatus.OK);
  }else{
   return new ResponseEntity<Void>(HttpStatus.NOT_FOUND);
   
  }
  
 }

}

We defined three endpoints: for listing, adding, and deleting. For demonstration purposes, I added some sample data in /resources forlder, which is also automatically picked by Spring Boot and applied to the database on startup.

insert into users values (1, 'user1', 'john', 'doe');
insert into users values (2, 'user2', 'long', 'beard');
insert into users values (3, 'user3', 'zakaria', 'amine');


Finally, we can launch our app by simply executing the Main class :

 
@SpringBootApplication
public class SpringbootDemoApplication {

 public static void main(String[] args) {
  SpringApplication.run(SpringbootDemoApplication.class, args);
 }
}
Our Rest API is all set. If we go to /users on a browser we get:


I created a html page with some Angular Js code to test the operations. Since the page was named "index.html",  Spring Boot automatically picked up the name and defined it as a welcome page.

I will not say more on the front end part since it is not the focus of this tutorial. You can find the source code here.

Total time in building all this and deploying it: less than 10mins.

Spring Boot is new way of getting things quickly into production, and it keeps on getting better with every release.

The whole application is available here:  https://github.com/zak905/springboot-demo.git

No comments:

Post a Comment