Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

306
Visualizações
Why getting only values instead of keys and values in JSON response

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?

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda