gpt4 book ai didi

java - 从 Spring Boot JSON 响应中删除 "_embedded"和 "_links"

转载 作者:行者123 更新时间:2023-12-02 09:39:12 27 4
gpt4 key购买 nike

我使用 CrudRepository 与 Restful 服务创建了一个基本的 Spring Boot 应用程序来与数据库交互。

JSON 响应包含我不想要的 "_embedded": { ... }"_links": { ... }

{
"_embedded" : {
"countries" : [
{
"name" : "Antarctica",
"_links" : {
"self" : {
"href" : "http://localhost:8080/world/rest/countries/ATA"
},
"country" : {
"href" : "http://localhost:8080/world/rest/countries/ATA"
}
}
},
...
]
}

如何摆脱这种结构并在简单列表中返回我的对象​​?是否可以设置某些属性或配置来将其从所有响应中删除?

我添加了属性spring.hateoas.use-hal-as-default-json-media-type=false,但不起作用。

尝试手动将结果转换为列表,countries.forEach(countriesList::add);;运气不好..

映射对象的 POJO 类

/* Package and Imports */

@Entity(name = "country")
public class Country {

@Id
private String code;

private String name;
private String continent;
private String region;

/* Getters, Setters and equals method below */
}

最佳答案

在 StackOverflow 和其他网站上浏览了许多主题之后。我得出的结论是,如果没有 spring-boot-starter-data-rest,我会过得更好。

  1. 我已从 pom.xml 中删除了 spring-boot-starter-data-rest 依赖项。
  2. 创建了一个 @RestController 来处理请求。
  3. 创建了处理不同请求类型(GET、POST 等)的方法
<小时/>

按照@michalk的建议

Disable Hypertext Application Language (HAL) in JSON?

这确实有部分帮助,但对数组没有帮助。

关于java - 从 Spring Boot JSON 响应中删除 "_embedded"和 "_links",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57234730/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com