我正在尝试从rest获得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
发布于 2022-05-31 20:13:09
好的,我认为发生这种情况的原因是:路径/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://stackoverflow.com/questions/72447898
复制相似问题