How to Create REST APIs with Spring Boot in a Full Stack Java App

In full stack Java development, REST APIs (Representational State Transfer) play a critical role in connecting the front-end and back-end of an application. These APIs enable seamless communication between user interfaces and servers, making your web apps dynamic and interactive. Among the various frameworks available, Spring Boot is the most popular choice for building robust and scalable RESTful web services in Java.


This article will guide you through the basics of creating REST APIs with Spring Boot as part of a full stack Java application.


What is Spring Boot?

Spring Boot is a framework built on top of the Spring framework that simplifies the development of Java-based applications. It eliminates the need for extensive configuration and allows developers to get started quickly with embedded servers, auto-configuration, and production-ready defaults.


When building a full stack Java app, Spring Boot typically handles the backend, while the front-end is developed using technologies like Angular, React, or Vue.js.


Steps to Create REST APIs with Spring Boot

1. Set Up Your Spring Boot Project

You can quickly bootstrap a Spring Boot application using Spring Initializr. Choose the following:


Project: Maven or Gradle


Language: Java


Dependencies: Spring Web, Spring Data JPA, H2/MySQL, and optionally Spring Boot DevTools


Download the project, import it into your IDE (like IntelliJ IDEA or Eclipse), and you’re ready to go.


2. Define the Model

Create a simple Java class that represents your data model. For example, a User model:


java

Copy

Edit

@Entity

public class User {

  @Id

  @GeneratedValue(strategy = GenerationType.IDENTITY)

  private Long id;

  private String name;

  private String email;


  // Getters and Setters

}

3. Create the Repository

Use Spring Data JPA to interact with the database.


java

Copy

Edit

public interface UserRepository extends JpaRepository<User, Long> {

}

4. Build the REST Controller

The controller is where you define the API endpoints:


java

Copy

Edit

@RestController

@RequestMapping("/api/users")

public class UserController {


  @Autowired

  private UserRepository userRepository;


  @GetMapping

  public List<User> getAllUsers() {

    return userRepository.findAll();

  }


  @PostMapping

  public User createUser(@RequestBody User user) {

    return userRepository.save(user);

  }


  @GetMapping("/{id}")

  public ResponseEntity<User> getUserById(@PathVariable Long id) {

    return userRepository.findById(id)

      .map(user -> ResponseEntity.ok().body(user))

      .orElse(ResponseEntity.notFound().build());

  }


  @PutMapping("/{id}")

  public ResponseEntity<User> updateUser(@PathVariable Long id, @RequestBody User userDetails) {

    return userRepository.findById(id)

      .map(user -> {

        user.setName(userDetails.getName());

        user.setEmail(userDetails.getEmail());

        return ResponseEntity.ok().body(userRepository.save(user));

      }).orElse(ResponseEntity.notFound().build());

  }


  @DeleteMapping("/{id}")

  public ResponseEntity<?> deleteUser(@PathVariable Long id) {

    return userRepository.findById(id)

      .map(user -> {

        userRepository.delete(user);

        return ResponseEntity.ok().build();

      }).orElse(ResponseEntity.notFound().build());

  }

}

5. Test Your API

You can test your APIs using tools like Postman or curl. If everything is set up correctly, your Spring Boot application will expose endpoints like:


GET /api/users – fetch all users


POST /api/users – create a new user


PUT /api/users/{id} – update a user


DELETE /api/users/{id} – delete a user


Conclusion

Spring Boot makes it easy to build RESTful APIs quickly and efficiently. When integrated with a front-end framework, these APIs act as the communication bridge between your client and server. As a full stack Java developer, mastering REST API creation with Spring Boot is essential for building modern, scalable web applications.

Read more

Get Directions

Comments

Popular posts from this blog

Best Testing Tools Training in Hyderabad – Master Software Testing

Full Stack Java Certification Programs in Hyderabad

Essential Skills Covered in Flutter Development Courses in Hyderabad