首页 > 解决方案 > 添加 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 usingJAXBContext`

从现在开始,API 调用的每个响应都返回带有标头的 XML 结构化对象content-type: application/xml

是否可以保持服务原样并仍使用默认 JSON 映射?

我不想放在produces = "application/json"每个端点上......

标签: springspring-boot

解决方案


@EnableWebMvc
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
    @Override
    public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) {
        configurer.defaultContentType(MediaType.APPLICATION_JSON);
    }
}

推荐阅读