我正在嘗試從其余 API 獲取 XML 回應。我從 API 獲得所有客戶的 JSON 回應和單個客戶的 XML。PFB 兩種情況下的螢屏列印:
案例 1:當 URL= http://localhost:8080/spring-crm-rest/api/customers/ 然后我得到 JSON 回應 JSON 回應
案例 2:當 URL = http://localhost:8080/spring-crm-rest/api/customers/1 然后我得到 XML 回應 XML 回應
請在 URL 下面找到完整的代碼,以便在您的最后復制相同的代碼。代碼鏈接:https ://drive.google.com/file/d/1fd7DyUsfOvY4fX0nm6j4fzrwxHyg9ZGz/view?usp=sharing
uj5u.com熱心網友回復:
好的,我認為發生這種情況的原因如下:路徑/spring-crm-rest/api/customers/具有回傳型別List<Customer>-> 默認 java 串列作為頂級 -> json 結果
雖然/spring-crm-rest/api/customers/1回傳型別Customer為頂級,其中包含 javax.xml.bind 注釋 -> xml 結果
更改此設定可能有點棘手,但您可以嘗試以下一些可能的方法:
- 像這樣專門設定端點的內容型別:
@GetMapping(produces = {"application/json"}) - 洗掉
@Xml.*注釋-> spring 將可以在沒有任何注釋的情況下序列化類,但沒有它們,您對生成的 json 的控制較少(例如重命名欄位等)。根據您的用例,可能不需要它
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/485911.html
