繁体   English   中英

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

[英]How to exclude other class objects in a GET request? - Java Spring Boot

我正在编写一个简单的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添加到getter之后,Game和Customer不再出现在Json响应中。 我仍然想知道是否有人可以帮助我使用JsonView,以及我需要采取哪些其他步骤才能不返回空对象“ {}”(在访问/ orders时)。 还更新了我的OrderController。

使用@JsonIgnore来响应忽略对象

@JsonIgnore
private Game game;

@JsonIgnore
private Customer customer;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM