Мой файл контроллера выглядит следующим образом:
Код: Выделить всё
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
//import org.springframework.web.bind.annotation.*;
import com.javatechie.crud.example.entity.Product;
import com.javatechie.crud.example.service.ProductService;
@RestController
public class ProductController {
@Autowired
private ProductService service;
@PostMapping("/addProduct")
public Product addProduct(@RequestBody Product product) {
return service.saveProduct(product);
}
@PostMapping("/addProducts")
public List
addProducts(@RequestBody List products) {
return service.saveProductAll(products);
}
@GetMapping("/products")
public List findAllProducts() {
return service.getProductAll();
}
@GetMapping("/productById/{id}")
public Product findProductById(@PathVariable int id) {
return service.getProductById(id);
}
@GetMapping("/product/{name}")
public Product findProductByName(@PathVariable String name) {
return service.getProductByName(name);
}
@DeleteMapping("/delete/{id}")
public String deleteProduct(@PathVariable int id) {
return service.deleteProduct(id);
}
}
Код: Выделить всё
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "PRODUCT_TBL")
public class Product {
@Id
@GeneratedValue
private int id;
private String name;
private int quantity;
private double price;
}
Код: Выделить всё
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.javatechie.crud.example.entity.Product;
import com.javatechie.crud.example.repository.ProductRepository;
@Service
public class ProductService {
@Autowired
private ProductRepository repository;
public Product saveProduct(Product product) {
return repository.save(product);
}
public List
saveProductAll(List products) {
return repository.saveAll(products);
}
public List getProductAll() {
return repository.findAll();
}
public Product getProductById(int id) {
return repository.findById(id).orElse(null);
}
public Product getProductByName(String name) {
return repository.findByName(name);
}
public String deleteProduct(int id) {
repository.deleteById(id);
return "Product havig id " +id +"deleted successfully";
}
}
Код: Выделить всё
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = india
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = create
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
server.port=9191
Ниже приведен мой класс репо:
Код: Выделить всё
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.javatechie.crud.example.entity.Product;
@Repository
public interface ProductRepository extends JpaRepository
{
Product findByName(String name);
}
Для вышеизложенного я получаю следующее ответ — {}
На следующий запрос GET я также получаю пустой ответ — http://localhost:9191/products
Я использую база данных mysql с ломбоком.
Пожалуйста, дайте мне знать, что происходит не так.
Подробнее здесь: https://stackoverflow.com/questions/769 ... pring-boot
Мобильная версия