I'm writing a simple GameShop Spring Boot CRUD service application. I have 3 classes Game, Customer, Order. The Order class has both the Game and Customer object.
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;
}
The problem is when I do a GET request like http://localhost:8080/orders it responds with the Game and Customer objects included as well like this:
"_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"
}
}
}
Meanwhile, I would like just to have links to them instead, for example http://localhost:8080/game/1 http://localhost:8080/customer/1 for the first order but I'm unsure how to exclude the objects themselves. I tried with @JsonView, but it just responds with and empty Json object.
Here is my controller for Order
@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);
}
}
Also, my View class
public class View {
public interface Summary {}
}
Also, I'm not using any database for now and have all of the object instances in static blocks of the CustomerService, OrderService, GameService classes.
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);
}
Lastly, I would still want when passed some type of argument to the GET request that it would show all the information(with the objects included)
I'm stuck for a few days now and have no other ideas. Any suggestions would be appreciated.
Thank you.
EDIT
After adding @JsonIgnore to the getters the Game and Customer do not appear in the Json response anymore. I would still like if anyone can help me with the JsonView and what additional steps do I need to take for it not to return an empty object "{}"(when accessing /orders). Also updated my OrderController.
Use @JsonIgnore for ignore object in response
@JsonIgnore
private Game game;
@JsonIgnore
private Customer customer;
在这种情况下,不要使用@JsonIgnore,只需创建一个单独的DTO,并包含所需的字段并在其中设置数据后返回即可。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.