I am trying to retrieve data from database using an SQL native query but I am getting only values instead of keys and values in JSON response. How can i retrieve data with key and value in Json response?
Entity:
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "Products")
public class ProductModel {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private Long id;
@Column(name = "Category")
private String category;
@Column(name = "ProductName")
@Lob
private String productName;
@Column(name = "Price")
private int price;
@Column(name = "Slider")
private Boolean slider;
@Column(name = "SpecialOffer")
private Boolean specialOffer;
@Column(name = "NewPrice")
private int newPrice;
@Column(name = "FullDescription")
@Lob
private String fullDescription;
@Column(name = "Image")
@Lob
private String image;
}
Repository:
@Repository
public interface ProductRepository extends JpaRepository<ProductModel, Long> {
@Query(value = "select id,image,product_name,price,new_price from products where slider is true", nativeQuery = true)
List<String> getSliderContent();
}
Service
@Service
public class ProductService {
@Autowired
private ProductRepository productRepository;
public List<String> getSliderContent(){
List<String> lstMovie = new ArrayList<>();
lstMovie = productRepository.getSliderContent();
return lstMovie;
}
}
Controller:
@CrossOrigin("*")
@RestController
@RequestMapping("/products")
public class ProductController {
@Autowired
private ProductService productService;
@GetMapping("/sl")
public List<String> getSliderContent(){
List<String> ls = productService.getSliderContent();
return ls;
}
}
I got data as json
["1, https://cdn.x-kom.pl/i/setup/images/prod/big/product-new-big, , 2021/10/pr_2021_10_8_14_22_27_699_08.jpg, Acer Swift 3 i5-1135G7/16GB/512/W11 Srebrny Intel Evo, 4800, 4500"]
Expected data in this format:
["id: 1, image: 'https://cdn.x-kom.pl/i/setup/images/prod/big/product-new-big, ,2021/10/pr_2021_10_8_14_22_27_699_08.jpg', product name: 'Acer Swift 3 i5-1135G7/16GB/512/W11 Srebrny Intel Evo', price: 4800, new_price: 4500"]
Can someone explain how to do it? I return data as String that's a problem, but how can i return Json object with native query?