我需要回傳帶有物體統計資訊的 DTO 回應,我可以使用介面回傳統計資訊,如下所示:
當前回應:
[
{
"name": "Customer 1",
"id": 1,
"countOrdersPending": 1,
"countOrdersCompleted": 2,
"countOrders": 3,
"totalAmount": 950.0
},
{
"name": "Customer 2",
"id": 2,
"countOrdersPending": 2,
"countOrdersCompleted": 3,
"countOrders": 5,
"totalAmount": 1867.5
}
]
我回傳統計屬性,但我需要回傳一個 customerStats 物件,如下所示:
預期回應:
[
{
"id": 1,
"name": "Customer 1",
"customerStats": {
"countOrdersPending": 1,
"countOrdersCompleted": 2,
"countOrders": 3,
"totalAmount": 950.0
}
},
{
"id": 2,
"name": "Customer 2",
"customerStats": {
"countOrdersPending": 2,
"countOrdersCompleted": 3,
"countOrders": 5,
"totalAmount": 1867.5
}
}
]
有人可以給我一個關于如何實作這個的提示嗎?
以下是課程:
客戶物體
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Entity
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "customers")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
}
訂單物體
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Entity
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "orders")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "customer_id", nullable = false, foreignKey = @ForeignKey(name = "fk_order_customer1"))
private Customer customer;
private Double amount;
@Enumerated(value = EnumType.STRING)
private OrderStatusType orderStatus;
}
訂單狀態型別列舉
public enum OrderStatusType {
PENDING,
COMPLETED
}
客戶統計回應介面
public interface CustomerStatsResponse {
Long getId();
String getName();
Double getTotalAmount();
Long getCountOrders();
Long getCountOrdersPending();
Long getCountOrdersCompleted();
}
客戶資料庫
import com.example.demo.dto.CustomerStatsResponse;
import com.example.demo.model.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
public interface CustomerRep extends JpaRepository<Customer, Long> {
@Query(value = "SELECT c.id, c.name, "
"SUM(o.amount) AS totalAmount, "
"COUNT(o.id) AS countOrders, "
"(SELECT COUNT(ord.id) FROM orders ord "
"WHERE ord.customer_id = c.id AND ord.order_status = 'PENDING') as countOrdersPending, "
"(SELECT COUNT(ord.id) FROM orders ord "
"WHERE ord.customer_id = c.id AND ord.order_status = 'COMPLETED') as countOrdersCompleted "
"FROM customers c "
"INNER JOIN orders o ON c.id = o.customer_id "
"GROUP BY c.id, c.name"
, nativeQuery = true)
List<CustomerStatsResponse> customerStats();
}
訂單倉庫
import com.example.demo.model.Order;
import org.springframework.data.jpa.repository.JpaRepository;
public interface OrderRep extends JpaRepository<Order, Long> {
}
客戶服務
import com.example.demo.dto.CustomerStatsResponse;
import com.example.demo.model.Customer;
import com.example.demo.repository.CustomerRep;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@RequiredArgsConstructor
public class CustomerService {
private final CustomerRep customerRep;
public List<Customer> findAll() {
return customerRep.findAll();
}
public List<CustomerStatsResponse> customerStats() {
return customerRep.customerStats();
}
}
訂購服務
import com.example.demo.model.Order;
import com.example.demo.repository.OrderRep;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@RequiredArgsConstructor
public class OrderService {
private final OrderRep orderRep;
public List<Order> findAll() {
return orderRep.findAll();
}
}
客戶控制器
import com.example.demo.dto.CustomerStatsResponse;
import com.example.demo.model.Customer;
import com.example.demo.service.CustomerService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("api/v1/customers")
@RequiredArgsConstructor
public class CustomerController {
private final CustomerService customerService;
@GetMapping
public ResponseEntity<List<Customer>> findAll() {
return ResponseEntity.ok(customerService.findAll());
}
@GetMapping("/stats")
public ResponseEntity<List<CustomerStatsResponse>> customerStats() {
return ResponseEntity.ok(customerService.customerStats());
}
}
訂單控制器
import com.example.demo.model.Order;
import com.example.demo.service.OrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("api/v1/orders")
@RequiredArgsConstructor
public class OrderController {
private final OrderService orderService;
@GetMapping
public ResponseEntity<List<Order>> findAll() {
return ResponseEntity.ok(orderService.findAll());
}
}
uj5u.com熱心網友回復:
只需創建一個CustomerStatsResponseDto匹配您想要的 JSON 并CustomerStatsResponse在其建構式中接受 a 的物件:
public class CustomerStatsResponseDto {
private Long id;
private String name;
private CustomerDto customerStats;
public CustomerStatsResponseDto(CustomerStatsResponse customerStatsResponse) {
this.id = customerStatsResponse.getId();
this.name = customerStatsResponse.getName();
this.customerStats = new CustomerStatsDto(customerStatsResponse.getTotalAmount(), customerStatsResponse.getCountOrders(),
customerStatsResponse.getCountOrdersPending(), customerStatsResponse.getCountOrdersCompleted());
}
}
public class CustomerStatsDto {
private Double totalAmount;
private Long countOrders;
private Long countOrdersPending;
private Long countOrdersCompleted;
public CustomerStatsDto(Double totalAmount, Long countOrders, Long countOrdersPending, Long countOrdersCompleted) {
this.totalAmount = totalAmount;
this.countOrders = countOrders;
this.countOrdersPending = countOrdersPending;
this.countOrdersCompleted = countOrdersCompleted;
}
}
現在,在你的CustomerController,你只需要從轉換CustomerStatsResponse到CustomerStatsResponseDto如下:
@RestController
@RequestMapping("api/v1/customers")
@RequiredArgsConstructor
public class CustomerController {
private final CustomerService customerService;
@GetMapping
public ResponseEntity<List<Customer>> findAll() {
return ResponseEntity.ok(customerService.findAll());
}
@GetMapping("/stats")
public ResponseEntity<List<CustomerStatsResponseDto>> customerStats() {
return ResponseEntity.ok(customerService.customerStats().stream()
.map(customerStatsResponse -> new CustomerStatsResponseDto(customerStatsResponse))
.collect(Collectors.toList()));
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/381716.html
