【发布时间】:2020-09-17 08:07:03
【问题描述】:
我想解析 json 列表。我用过springboot项目,数据库是H2。数据库包含少量数据。
我已经通过@getmapping 函数成功展示了json数据。 代码如下,
File-----> ProductController.Java
@GetMapping("/products_from_db")
public List<Product> getProducts_from_db() {
return productservice.findAll() ;
}
第二个文件如下。
File ----> Product.java
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
//@ToString
@Entity
public class Product {
@Id
@GeneratedValue
private Integer id;
private String name;
private int price;
@Override
public String toString() {
return "Product{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
'}';
}
输出:
[{
"id": 1,
"name": "Mango",
"price": 98
}, {
"id": 2,
"name": "Strawberry",
"price": 92
}, {
"id": 3,
"name": "Cadbury",
"price": 85
}, {
"id": 4,
"name": "yash",
"price": 100
}]
我只想在后端代码中显示名称和价格。
【问题讨论】:
标签: java json spring-boot parsing gson