-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeek-04.java
More file actions
65 lines (63 loc) · 2.82 KB
/
Copy pathWeek-04.java
File metadata and controls
65 lines (63 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
Week-04
---------------------------------------------------------------------------------------------
1. Understanding React Frontend
---------------------------------------------------------------------------------------------
2. Setting Up a Spring Boot Project with Models
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private Double price;
// Getters and Setters
}
---------------------------------------------------------------------------------------------
3. Loading Data into H2 Database
spring.datasource.url=jdbc:h2:mem:testdb
spring.h2.console.enabled=true
---------------------------------------------------------------------------------------------
4. Handling CORS Errors in Spring Boot
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("http://localhost:3000");
}
}
---------------------------------------------------------------------------------------------
5. Using ResponseEntity and Fetching Data by ID
@GetMapping("/{id}")
public ResponseEntity<Product> getProductById(@PathVariable Long id) {
Optional<Product> product = productRepository.findById(id);
return product.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
---------------------------------------------------------------------------------------------
6. Adding Products with Images
@PostMapping("/products")
public Product addProduct(@RequestParam("file") MultipartFile file, @RequestParam("name") String name) {
// Logic to save product and file
}
---------------------------------------------------------------------------------------------
7. Fetching Images from the Server
@GetMapping("/products/{id}/image")
public ResponseEntity<byte[]> getProductImage(@PathVariable Long id) {
byte[] image = productService.getProductImage(id);
return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(image);
}
---------------------------------------------------------------------------------------------
8. Updating and Deleting Products@PutMapping("/{id}")
public Product updateProduct(@PathVariable Long id, @RequestBody Product product) {
return productService.updateProduct(id, product);
}
@DeleteMapping("/{id}")
public void deleteProduct(@PathVariable Long id) {
productService.deleteProduct(id);
}
---------------------------------------------------------------------------------------------
9. Implementing a Search Feature
public interface ProductRepository extends JpaRepository<Product, Long> {
List<Product> findByNameContaining(String name);
}
---------------------------------------------------------------------------------------------