Spring 添加xml支持后,响应的内容类型设置为application/xml
我的另一个棘手的问题。 我有rest api控制器:Spring 添加xml支持后,响应的内容类型设置为application/xml,spring,spring-boot,Spring,Spring Boot,我的另一个棘手的问题。 我有rest api控制器: @RestController() @RequestMapping("/api/project") public class ProjectController{ @PreAuthorize("hasAuthority('ROLE_USER')") @GetMapping(value = "/{id}/severitychart") public ResponseEntity<HashMap<String,
@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);
}
}
@RestController()
@请求映射(“/api/project”)
公共类项目控制器{
@预授权(“hasAuthority('ROLE_USER'))
@GetMapping(value=“/{id}/severitychart”)
公共响应属性showSeverityChart(@PathVariable(“id”)长id){
返回projectService.showSeverityChart(id);
}
}
它工作得很好,返回了JSON
响应
后来,我不得不添加XML
的处理——没什么特别的,只是简单地解析给定的XML
消息并将其存储到数据库中。不过,所有响应都应该映射到JSON,这是使用
JAXBContext完成的`
从现在起,API调用的每个响应都返回XML结构化对象,标题为内容类型:application/XML
是否可以保持服务原样并仍然使用默认的JSON映射
我不想在每个端点上放置products=“application/json”
@EnableWebMvc
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
@Override
public void configureContentNegotiation(final ContentNegotiationConfigurer configurer) {
configurer.defaultContentType(MediaType.APPLICATION_JSON);
}
}