I am trying to use dependency injection in Java Spring Boot. Receiving Error below in controller in this line. productService.updateProduct(product);
Error: Change 1st parameter of abstract method from Product to Optional
How can this be resolved?
public class Product {
@Getter @Setter public @Id @GeneratedValue Long productId;
@Getter @Setter public String productName;
@Getter @Setter public String productDescription;
Product() {}
Product(String productName, String productDescription) {
this.productName = productName;
this.productDescription = productDescription;
}
}
Interface:
public interface IProductService {
public Product updateProduct(Product product);
}
Service:
@Service
public class ProductService implements IProductService {
@Override
public Product updateProduct(Product product){
product.productName = "test12345";
return product;
}
}
Controller:
class ProductController {
ProductRepository repository;
@Autowired IProductService productService;
ProductController(IProductService productService, ProductRepository repository) {
this.productService = productService;
this.repository = repository;
}
@GetMapping("/products/{id}")
Product one(@PathVariable Long id) {
var product = repository.findById(id);
var finalProduct = productService.updateProduct(product); // error in this line
return finalProduct;
}