<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.
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() {}
}
@Controller
, @RequestMapping
annotations @Controller
@RequestMapping(value = "api")
public class UserController {
}
private static List<User> users = new ArrayList<User>();
public UserController() {
User u1 = new User(1L, "shijazi", "password", "[email protected]", "Safwan", "Hijazi");
User u2 = new User(2L, "test", "password", "[email protected]", "test", "test");
users.add(u1);
users.add(u2);
}
@RequestMapping(value = "users", method = RequestMethod.GET)
public @ResponseBody List<User> getAllUsers() {
return users;
}
Run the application [by mvn clean install spring-boot:run
] and call this URL http://localhost:8080/api/users
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", "[email protected]", "Safwan", "Hijazi");
User u2 = new User(2L, "test", "password", "[email protected]", "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