Spring Boot Quickstart
Spring Boot Quickstart
1. Install Prerequisites
Make sure you have the following installed:
- Java Development Kit (JDK): Version 8 or higher. Check if it's installed:
bash
java -version
Download it from oracle.com if necessary.
- Maven: A build tool for Java projects. Check if it's installed:
bash
mvn -version
Download it from maven.apache.org if needed.
2. Create a New Spring Boot Project
Use the Spring Initializr to generate a new Spring Boot project:
- Go to start.spring.io.
- Choose Maven Project.
- Select your preferred Java version.
- Fill in the Group and Artifact (e.g.,
com.example
anddemo
). - Select Dependencies:
- Spring Web
- Spring Data JPA
- H2 Database (for an in-memory database)
- Click Generate to download a ZIP file containing your project.
3. Extract and Navigate to Your Project
Unzip the downloaded file and navigate to the project directory:
cd demo
4. Open the Project in Your IDE
Open the project in your favorite IDE (like IntelliJ IDEA or Eclipse). If using IntelliJ, import the project as a Maven project.
5. Create a Simple Model
In src/main/java/com/example/demo
, create a class called Item.java
:
package com.example.demo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String description;
// Getters and setters
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
}
6. Create a Repository
Create an interface called ItemRepository.java
:
package com.example.demo;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ItemRepository extends JpaRepository<Item, Long> {
}
7. Create a Controller
Create a class called ItemController.java
:
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/items")
public class ItemController {
@Autowired
private ItemRepository itemRepository;
@GetMapping
public List<Item> getAllItems() {
return itemRepository.findAll();
}
@PostMapping
public Item createItem(@RequestBody Item item) {
return itemRepository.save(item);
}
}
8. Run the Application
Run the main class located at src/main/java/com/example/demo/DemoApplication.java
:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
You can run the application via your IDE or using Maven:
mvn spring-boot:run
9. Test Your API with Postman
GET All Items:
- Open Postman.
- Set the request type to GET.
- Enter the URL:
http://localhost:8080/items
. - Click Send.
- You should see an empty array (
[]
) as no items have been added yet.
POST a New Item:
- Change the request type to POST.
- Enter the URL:
http://localhost:8080/items
. - Go to the Body tab.
- Select raw and choose JSON from the dropdown.
- Enter the following JSON:
json
{
"name": "Item1",
"description": "This is Item 1"
}
- Click Send.
- You should see the created item in the response, including its ID.
Verify Item Creation:
- Set the request type to GET.
- Enter the URL:
http://localhost:8080/items
. - Click Send.
- You should see the list with your newly created item.
Conclusion
You've successfully created a Spring Boot application with a model, repository, and RESTful controller. You also tested your API using Postman.
For more detailed documentation, visit the official Spring Boot documentation at spring.io. Happy coding!