我正在编写一个简单的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对象。
这是我的订单管理员
@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类的静态块中。
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添加到吸气剂之后,游戏和客户不再出现在Json响应中。我仍然想知道是否有人可以帮助我使用JsonView,以及我需要采取哪些其他步骤才能不返回空对象“ {}”(在访问/ orders时)。还更新了我的OrderController。
答案 0 :(得分:0)
在响应中使用@JsonIgnore忽略对象
@JsonIgnore
private Game game;
@JsonIgnore
private Customer customer;
答案 1 :(得分:0)
在这种情况下,请勿使用@JsonIgnore,只需创建一个单独的DTO,并包含所需的字段并在其中设置数据后返回即可。