Spring Boot实战:在线购物系统API如何返回一对多嵌套JSON?
Hey there! Since you're new to Spring Boot and API development, let's walk through exactly how to create the nested JSON response you need for your practice online shopping system. I'll keep this straightforward so you can follow along easily.
Step 1: Set Up Your Spring Boot Project
First, create a new Spring Boot project (you can use Spring Initializr locally or via its web interface—just make sure to select the Spring Web dependency, which gives you all the tools to build REST APIs).
Step 2: Create Your Entity Classes
We need two classes to map the nested structure in your JSON:
OrderDetailfor the innerdetailsarray itemsProductfor the top-level objects
Add the Lombok dependency to your pom.xml (or build.gradle) if you want to skip writing getters/setters manually—it's a huge time-saver for practice projects!
OrderDetail Class
import lombok.Data; @Data public class OrderDetail { private Integer orderId; private Integer productId; private Integer quantity; }
Product Class
import lombok.Data; import java.util.List; @Data public class Product { private Integer id; private String name; private String description; private Double price; // Or use BigDecimal for more accurate currency handling private List<OrderDetail> details; }
Note: If you don't want to use Lombok, just add manual getter and setter methods for all fields in both classes.
Step 3: Build a REST Controller
Create a controller class that will return your mock product data as JSON. This will be your API endpoint.
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class ProductController { @GetMapping("/products") public List<Product> getProducts() { // Create mock order details for product 1 OrderDetail detail1 = new OrderDetail(); detail1.setOrderId(10); detail1.setProductId(1); detail1.setQuantity(4); OrderDetail detail2 = new OrderDetail(); detail2.setOrderId(12); detail2.setProductId(1); detail2.setQuantity(5); // Create product 1 Product product1 = new Product(); product1.setId(1); product1.setName("pname_46"); product1.setDescription("pdesc_793_793_793_79"); product1.setPrice(519.95); product1.setDetails(List.of(detail1, detail2)); // Create mock order details for product 2 OrderDetail detail3 = new OrderDetail(); detail3.setOrderId(20); detail3.setProductId(2); detail3.setQuantity(3); // Adjust quantity as needed Product product2 = new Product(); product2.setId(2); product2.setName("pname_608"); product2.setDescription("pdesc_874_874_874"); product2.setPrice(221.7); product2.setDetails(List.of(detail3)); // Return list of products return List.of(product1, product2); } }
Step 4: Test Your API
Start your Spring Boot application, then open a browser or a tool like Postman and navigate to http://localhost:8080/products. You'll see the exact nested JSON structure you asked for!
A few quick tips for next steps:
- Later, you can connect a practice database (like H2) and replace mock data with real database queries using Spring Data JPA.
- For currency, consider switching
DoubletoBigDecimalto avoid floating-point precision issues. - You can add more endpoints (like POST to add products) as you get comfortable with Spring Boot.
内容的提问来源于stack exchange,提问作者Tillo

