集成Eureka后Controller返回结果为xml的解决方案 - yunze-gh/blog-cloud GitHub Wiki
SpringBoot项目在集成了Eureka之后,Controller接口返回的信息变成了xml格式的,但现在一般都是使用的json来进行数据交互,所以记录下解决方案。
@GetMapping(value = "all")
public Result all() {
Demo byId = service.getById("1");
return new Result(CodeEnum.SUCCESS.get(), byId, LocalDateTime.now().toString());
}
此时pom.xml中的相关依赖如下:
<!-- eureka [start] -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- eureka [end] -->
在接口的GetMapper注解上加上如下属性即可
produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE
@GetMapping(value = "all", produces = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
public Result all() {
Demo byId = service.getById("1");
return new Result(CodeEnum.SUCCESS.get(), byId, LocalDateTime.now().toString());
}
接口多了之后,每个接口上加produces也比较麻烦,所以修改eureka依赖为:spring-cloud-starter-netflix-eureka-client
<!-- eureka [start] -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- eureka [end] -->
Controller接口无需改动
@GetMapping(value = "all")
public Result all() {
Demo byId = service.getById("1");
return new Result(CodeEnum.SUCCESS.get(), byId, LocalDateTime.now().toString());
}