spring-boot Creating a REST-Service


Example

  1. Create project using STS (Spring Starter Project) or Spring Initializr (at https://start.spring.io ).
  2. Add a Web Dependency in your pom.xml:
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

or type web in Search for dependencies search box, add web dependency and download zipped project.

  1. Create a Domain Class (i.e. User)
 public class User {
    
        private Long id;
    
        private String userName;
    
        private String password;
    
        private String email;
    
        private String firstName;
    
        private String lastName;
    
        public Long getId() {
            return id;
        }
    
        public void setId(Long id) {
            this.id = id;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public String getFirstName() {
            return firstName;
        }
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
        @Override
        public String toString() {
            return "User [id=" + id + ", userName=" + userName + ", password=" + password + ", email=" + email
                    + ", firstName=" + firstName + ", lastName=" + lastName + "]";
        }
    
        public User(Long id, String userName, String password, String email, String firstName, String lastName) {
            super();
            this.id = id;
            this.userName = userName;
            this.password = password;
            this.email = email;
            this.firstName = firstName;
            this.lastName = lastName;
        }
    
        public User() {}
    }
  1. Create UserController class and add @Controller, @RequestMapping annotations
    @Controller
    @RequestMapping(value = "api")
    public class UserController {
    }
  1. Define static List users variable to simulate database and add 2 users to the list
    private static List<User> users = new ArrayList<User>();

    public UserController() {
        User u1 = new User(1L, "shijazi", "password", "shijazi88@gmail.com", "Safwan", "Hijazi");
        User u2 = new User(2L, "test", "password", "test@gmail.com", "test", "test");
        users.add(u1);
        users.add(u2);
    }
  1. Create new method to return all users in static list (getAllUsers)
    @RequestMapping(value = "users", method = RequestMethod.GET)
    public @ResponseBody List<User> getAllUsers() {
         return users;
    }
  1. Run the application [by mvn clean install spring-boot:run] and call this URL http://localhost:8080/api/users

  2. We can annotate the class with @RestController, and in this case we can remove the ResponseBody from all methods in this class, (@RestController = @Controller + ResponseBody), one more point we can control the return http code if we use ResponseEntity, we will implement same previous functions but using @RestController and ResponseEntity

@RestController
@RequestMapping(value = "api2")
public class UserController2 {

    private static List<User> users = new ArrayList<User>();

    public UserController2() {
        User u1 = new User(1L, "shijazi", "password", "shijazi88@gmail.com", "Safwan", "Hijazi");
        User u2 = new User(2L, "test", "password", "test@gmail.com", "test", "test");
        users.add(u1);
        users.add(u2);
    }
    
    @RequestMapping(value = "users", method = RequestMethod.GET)
    public ResponseEntity<?> getAllUsers() {
       try {
           return new ResponseEntity<>(users, HttpStatus.OK);
       } catch (Exception e) {
           return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
       }
    }
}

now try to run the application and call this URL http://localhost:8080/api2/users