gpt4 book ai didi

java - 如何在 GET 请求中排除其他类对象? - Java Spring启动

转载 作者:行者123 更新时间:2023-12-02 01:37:37 24 4
gpt4 key购买 nike

我正在编写一个简单的 GameShop Spring Boot CRUD 服务应用程序。我有 3 类游戏、客户、订单。 Order 类同时具有 Game 和 Customer 对象。

public class Game {

@JsonView(View.Summary.class)
private int id;
@JsonView(View.Summary.class)
private String name;
private String genre;
private String platform;
@JsonView(View.Summary.class)
private String price;
private String developer;
}

public class Customer {

@JsonView(View.Summary.class)
private int id;
@JsonView(View.Summary.class)
private String name;
@JsonView(View.Summary.class)
private String email;
private String phoneNumber;
}

public class Order {

@JsonView(View.Summary.class)
private int id;
@JsonView(View.Summary.class)
private Date orderDate; // Mby string idk
@JsonView(View.Summary.class)
private boolean completed;
private Game game;
private Customer customer;
}

问题是当我执行像 http://localhost:8080/orders 这样的 GET 请求时它会响应包含的 Game 和 Customer 对象,如下所示:

"_embedded": {
"orderList": [
{
"id": 1,
"orderDate": "2019-03-04T20:12:20.207+0000",
"completed": false,
"game": {
"id": 1,
"name": "Persona 5",
"genre": "JRPG",
"platform": "PS4",
"price": "59.99",
"developer": "Atlus"
},
"customer": {
"id": 1,
"name": "Jonas",
"email": "Jonas123@gmail.com",
"phoneNumber": "867492455"
}
},
{
"id": 2,
"orderDate": "2019-03-04T20:12:20.207+0000",
"completed": false,
"game": {
"id": 2,
"name": "Red dead redemption 2",
"genre": "Action Adventure",
"platform": "PS4",
"price": "59.99",
"developer": "Rockstar"
},
"customer": {
"id": 2,
"name": "Petras",
"email": "Petras123@gmail.com",
"phoneNumber": "867296545"
}
},
{
"id": 3,
"orderDate": "2019-03-04T20:12:20.207+0000",
"completed": false,
"game": {
"id": 3,
"name": "XCOM 2",
"genre": "Strategy",
"platform": "PC",
"price": "49.99",
"developer": "Dev3"
},
"customer": {
"id": 3,
"name": "Zilvinas",
"email": "Zilve123@gmail.com",
"phoneNumber": "869444455"
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/orders"
}
}

}

同时,我只想提供指向它们的链接,例如 http://localhost:8080/game/1 http://localhost:8080/customer/1对于第一个订单,但我不确定如何排除对象本身。我尝试使用 @JsonView,但它只响应空 Json 对象。

这是我的订单 Controller

@RestController
public class OrderController {

@Autowired
private OrderService orderService;

//@JsonView(View.Summary.class) // commented out because gives empty response
@GetMapping("/orders")
public ResponseEntity<Resources<Order>>getAllOrders(){

List<Order> orders = orderService.getAllOrders();
Resources<Order> resources = new Resources<Order>(orders);
Link linkTo =
linkTo(methodOn(this.getClass()).getAllOrders()).withSelfRel();
resources.add(linkTo);
return ResponseEntity.ok(resources);
}

@GetMapping("/orders/{orderId}")
public Order getOrderById(@PathVariable int orderId) {
return orderService.getOrderById(orderId);
}
}

另外,我的 View 类

public class View {
public interface Summary {}
}

此外,我目前没有使用任何数据库,并且所有对象实例都位于 CustomerService、OrderService、GameService 类的静态 block 中。

private static List <Order> orders = new ArrayList<Order>();
private static List <Game> games = new ArrayList<Game>();
private static List <Customer> customers = new ArrayList<Customer>();

static {
GameService gameService = new GameService();
CustomerService customerService = new CustomerService();
games = gameService.getAllGames();
customers = customerService.getAllCustomers();

Order order1 = new Order(1,new Date(),false,games.get(0),customers.get(0));
Order order2 = new Order(2,new Date(),false,games.get(1),customers.get(1));
Order order3 = new Order(3,new Date(),false,games.get(2),customers.get(2));

orders.add(order1);
orders.add(order2);
orders.add(order3);

}

最后,我仍然希望在将某种类型的参数传递给 GET 请求时它会显示所有信息(包含对象)

我被困了几天了,没有其他想法。如有任何建议,我们将不胜感激。

谢谢。

编辑

将 @JsonIgnore 添加到 getter 后,游戏和客户不再出现在 Json 响应中。我仍然希望是否有人可以帮助我使用 JsonView 以及我需要采取哪些额外步骤才能使其不返回空对象“{}”(访问/订单时)。还更新了我的 OrderController。

最佳答案

在这种情况下,不要使用 @JsonIgnore,只需创建一个单独的 DTO 并包含您想要的字段,并在其中设置数据后返回。

关于java - 如何在 GET 请求中排除其他类对象? - Java Spring启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54991429/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com