spring - 添加 xml 支持后,响应的内容类型设置为 application/xml
问题描述
我的另一个奇怪的问题。我有休息 api 的控制器:
@RestController()
@RequestMapping("/api/project")
public class ProjectController{
@PreAuthorize("hasAuthority('ROLE_USER')")
@GetMapping(value = "/{id}/severitychart")
public ResponseEntity<HashMap<String,Long>> showSeverityChart(@PathVariable("id")Long id) {
return projectService.showSeverityChart(id);
}
}
它工作得很好,恢复了JSON
响应。
后来我不得不添加 proccessing of XML
- 没什么特别的,只是简单地解析给定的XML
消息并将其存储到数据库中。仍然应该将所有响应映射到 JSON it was done using
JAXBContext`
从现在开始,API 调用的每个响应都返回带有标头的 XML 结构化对象content-type: application/xml
是否可以保持服务原样并仍使用默认 JSON 映射?
我不想放在produces = "application/json"
每个端点上......
解决方案
@EnableWebMvc
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
@Override
public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) {
configurer.defaultContentType(MediaType.APPLICATION_JSON);
}
}
推荐阅读
- oauth - 开放身份和 OAuth
- python - 如何根据两个参数计算熊猫数据框中的实例(使用带有“和”运算符的 lambda 函数)?
- pandas - 无法在pycharm中绘图
- c# - 单元测试场景中的游戏对象
- python - 熊猫合并不考虑常见项目
- azure - 使用预定义的连接器将 LogicApp 连接到 D365 CRM
- javascript - How to get the inner object array in javascript?
- outlook - 如何在 vsto Outlook 加载项中实现默认快捷方式?
- android - 如何在 Android Oreo 及更高版本的通知托盘中显示带有图片的 Android 通知?
- php - jQuery Datepicker 在每个月的前 10 天不显示事件