在boot2.x集成eureka后发现控制层接口返回的数据格式由json格式变成xml格式了,我就纳闷了。
界面数据展示
相关请求响应头
控制层代码
@GetMapping("find/{deptNo}")
@ResponseBody
// @HystrixCommand(fallbackMethod = "HystrixDep")//服务熔断
public Dept find(@PathVariable("deptNo") Integer deptNo) {
Dept dept = deptService.findById(deptNo);
if (null == dept) {
throw new RuntimeException("没有找到该id对应的信息");
}
return dept;
}
经过百度后发现是由该依赖引起的
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
但是我没有手动引入该依赖,应该是其他依赖自动引入的。经过排查后发现是Eureka依赖自动引入的,可以看一下依赖关系
所以只需要在eureka依赖中使用 排除该依赖就行。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</exclusion>
</exclusions>
</dependency>
记得关闭项目后点击刷新按钮再启动
重启项目后访问接口返回是json格式数据