我使用spring data rest将实体公开为(分页的)rest资源.一切正常,但当我通过RestTemplate请求数据时,我得到了一个无用的HATEOAS JSON(我没有要求).JSON似乎是一个页面资源.我可以接受,但JSON没有正确地转换成对象.里面没有content.

Repository:

@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long>
{
    List<Person> findByLastName(@Param("name") String name);
}

客户:

public List<Person> getPersons()
{
    RestTemplate rt = new RestTemplate();
    System.out.println(rt.getForObject(URL, PagedResources.class).getContent().size());
    System.out.println(rt.getForObject(URL, PagedResources.class).getLinks().size());
    System.out.println(rt.getForObject(URL, PagedResources.class).getMetadata().getTotalElements());
    return new ArrayList<Person>(rt.getForObject(URL, PagedResources.class).getContent()); // <-- empty
}

System.out:

0 // getContent().size()
4 // getLinks().size()
2 // getTotalElements()

curl :

C:\...>curl http://localhost:8080/spring-jsf-rest/rest/people
{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/spring-jsf-rest/rest/people{?page,size,sort}",
      "templated" : true
    },
    "search" : {
      "href" : "http://localhost:8080/spring-jsf-rest/rest/people/search"
    }
  },
  "_embedded" : {
    "people" : [ {
      "firstName" : "John",
      "lastName" : "Rambo",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/spring-jsf-rest/rest/people/1"
        }
      }
    }, {
      "firstName" : "Chuck",
      "lastName" : "Norris",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/spring-jsf-rest/rest/people/2"
        }
      }
    } ]
  },
  "page" : {
    "size" : 20,
    "totalElements" : 2,
    "totalPages" : 1,
    "number" : 0
  }
}

It seems like _embedded is not mapped correctly to content?!

推荐答案

As you've discovered correctly, PagedResources does not have an _embedded property, that's why you don't get the content property populated.

这一困境可以通过两种不同的方式解决:

  1. 首先提供与表示形式匹配的类型.因此,创建一个自定义类,要么坚持表示的属性名称,要么使用Jackson注释等对其进行自定义.

  2. Set up a custom MappingJackson2HttpMessageConverter and customize the ObjectMapperto get the Jackson2HalModule configured that Spring HATEOAS ships out of the box.

    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.registerModule(new Jackson2HalModule());
    
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setSupportedMediaTypes(MediaType.parseMediaTypes("application/hal+json"));
    converter.setObjectMapper(mapper);
    
    RestTemplate template = new RestTemplate(Collections.<HttpMessageConverter<?>> singletonList(converter));
    

Json相关问答推荐

使用Shell深入挖掘到最低的SON元素

Swift解码错误类型与`Bool`type不一致

如何编写MongoDB查询以返回数组数组

如何创建生成两个不同对象的JSON数组的SQL查询?

处理输入数据并转换为更简单的格式-PowerBI

通过在织女星简化图上裁剪来显示文本

无法从JSON解析ZonedDateTime,但可以使用格式化程序很好地解析

在MongoDB中检索某个月份和年份的JSON文档

解析Ansible AWS ec2_security_group中的json以提取安全组ID

使用 Groovy 将 XML 转换为 JSON

VBA-JSON 嵌套集合解析为 Excel

打印与 JSON 和 PowerShell 中的模式匹配的子项的父项名称

try 使用 JQ 转换 JSON 中过于复杂的对象数组

如何使用 Google 表格应用程序脚本将 JSON 中的多个字段提取到 Google 表格中

派生类的属性没有得到价值

JOLT 在 struct 体中间添加一个 JSON 字段

如何在 Django 的模板语言中获取 json 键和值?

如何在golang中获取 struct 的json字段名称?

如何使用 Newtonsoft.Json 反序列化 JSON 数组

JSON.stringify 向我的 Json 对象添加额外的 \ 和 "" 的问题