0% found this document useful (0 votes)
7 views5 pages

Spring Boot REST API Example Code

The document outlines a Spring Boot REST API project with Maven configuration, including dependencies for web, data JPA, and H2 database. It provides application properties for H2 console and JPA settings, along with a Product model, repository, and controller for CRUD operations. The main application class initializes the Spring Boot application.

Uploaded by

harshshekhada246
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Spring Boot REST API Example Code

The document outlines a Spring Boot REST API project with Maven configuration, including dependencies for web, data JPA, and H2 database. It provides application properties for H2 console and JPA settings, along with a Product model, repository, and controller for CRUD operations. The main application class initializes the Spring Boot application.

Uploaded by

harshshekhada246
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Code:-

[Link] (Maven Dependencies)

<project xmlns="[Link] ...>


<modelVersion>4.0.0</modelVersion>
<groupId>[Link]</groupId>
<artifactId>rest-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>REST API</name>
<description>Spring Boot REST API Example</description>
<properties>
<[Link]>17</[Link]>
</properties>
<dependencies>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Spring Boot Starter Data JPA -->


<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<!-- H2 in-memory database -->


<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<!-- Spring Boot Test -->


<dependency>
<groupId>[Link]</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

[Link]

# H2 Console
[Link]=true
[Link]=/h2-console

# JPA settings
[Link]=jdbc:h2:mem:testdb
[Link]=[Link]
[Link]=sa
[Link]=
[Link]-platform=[Link].H2Dialect
[Link]-auto=update
[Link]-sql=true

Model: [Link]

package [Link];

import [Link].*;

@Entity
public class Product {
@Id
@GeneratedValue(strategy = [Link])
private Long id;

private String name;


private Double price;

// Constructors
public Product() {}

public Product(String name, Double price) {


[Link] = name;
[Link] = price;
}

// Getters & Setters


public Long getId() {
return id;
}

public String getName() {


return name;
}

public void setName(String name) {


[Link] = name;
}

public Double getPrice() {


return price;
}

public void setPrice(Double price) {


[Link] = price;
}
}

Repository: [Link]

package [Link];

import [Link];
import [Link];

public interface ProductRepository extends JpaRepository<Product,


Long> {
}

Controller: [Link]

package [Link];

import [Link];
import [Link];
import [Link];
import [Link].*;

import [Link];
import [Link];

@RestController
@RequestMapping("/api/products")
public class ProductController {

@Autowired
private ProductRepository productRepo;

// Create Product
@PostMapping
public Product createProduct(@RequestBody Product product) {
return [Link](product);
}

// Get All Products


@GetMapping
public List<Product> getAllProducts() {
return [Link]();
}

// Get Product by ID
@GetMapping("/{id}")
public Optional<Product> getProductById(@PathVariable Long id) {
return [Link](id);
}
// Update Product
@PutMapping("/{id}")
public Product updateProduct(@PathVariable Long id, @RequestBody
Product
productDetails) {
Product product = [Link](id).orElseThrow();
[Link]([Link]());
[Link]([Link]());
return [Link](product);
}

// Delete Product
@DeleteMapping("/{id}")
public void deleteProduct(@PathVariable Long id) {
[Link](id);
}
}

Main Class: [Link]

package [Link];

import [Link];
import [Link];

@SpringBootApplication
public class RestApiApplication {
public static void main(String[] args) {
[Link]([Link], args);
}
}

You might also like