【问题标题】:How to consume Spring HATEOAS REST resources containing links to another resources?如何使用包含指向另一个资源的链接的 Spring HATEOAS REST 资源?
【发布时间】:2023-03-29 00:44:01
【问题描述】:

我在服务器上有/studentCourses 端点(使用 Spring Data REST 构建),它返回以下内容:

{
  "_embedded" : {
    "studentCourses" : [
      {
        "uid" : "5f23abe9-b24e-4e76-86b0-d539950a0a41",
        "registrationDate" : "7/23/2016",
        "_links" : {
          "self" : {
            "href" : "http://localhost:8080/studentCourses/5f23abe9-b24e-4e76-86b0-d539950a0a41"
          },
          "studentCourse" : {
            "href" : "http://localhost:8080/studentCourses/5f23abe9-b24e-4e76-86b0-d539950a0a41"
          },
          "course" : {
            "href" : "http://localhost:8080/studentCourses/5f23abe9-b24e-4e76-86b0-d539950a0a41/course"
          },
          "student" : {
            "href" : "http://localhost:8080/studentCourses/5f23abe9-b24e-4e76-86b0-d539950a0a41/student"
          }
        }
      },
      {
        ...
      },
      ...
    ]
  },
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/studentCourses"
    },
    "profile" : {
      "href" : "http://localhost:8080/profile/studentCourses"
    }
  },
  "page" : {
    ...
  }
}

以及以下客户端代码:

class StudentCourseDTO {

    String uuid;

    String registrationDate;

    StudentDTO student; // contains uuid, firstName, lastName, etc.

    CourseDTO course; // contains uuid, name, etc.

    // getters, setters

}

RestTemplate restTemplate() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new Jackson2HalModule());
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    MappingJackson2HttpMessageConverter messageConverter =
            new MappingJackson2HttpMessageConverter();
    messageConverter.setObjectMapper(objectMapper);
    messageConverter.setSupportedMediaTypes(Arrays.asList(MediaTypes.HAL_JSON));
    return new RestTemplate(Arrays.asList(messageConverter));
}

...

Collection<StudentCourseDTO> studentCourses = restTemplate().exchange(
        "http://localhost:8080/studentCourses",
        HttpMethod.GET, null,
        new ParameterizedTypeReference<PagedResources<StudentCourseDTO>>() {})
        .getBody().getContent();

问题是StudentCourseDTO.studentStudentCourseDTO.course 始终是null,但StudentCourseDTO.uuidStudentCourseDTO.registrationDate 可以从服务器正确检索。
有人知道我错过了什么吗?
我认为必须有某种方式告诉RestTemplate 自动跟踪返回内容中的链接,如上例中的studentcourse,但我还没有找到这样做的方法。

【问题讨论】:

  • 我建议不要自动关注这些链接。您可能只想获取所需的信息,以减少 IO、减少服务器负载和整体简单性。

标签: java spring jackson spring-data-rest spring-hateoas


【解决方案1】:

仅仅因为有链接并不意味着它们会被自动关注。

我会将StudentCourseDTO 更改为:

class StudentCourseDTO {

    String uuid;

    String registrationDate;

}

然后你会将响应反序列化为

PagedResources<Resource<StudentCourseDTO>> studentCourses = restTemplate().exchange(
        "http://localhost:8080/studentCourses",
        HttpMethod.GET, null,
        new ParameterizedTypeReference<PagedResources<Resource<StudentCourseDTO>>>() {})
        .getBody().getContent();

对于每个Resource&lt;StudentCourseDTO&gt;,您可以点击studentcourse的链接,例如通过使用RestTemplate 检索资源。

当然,这会给每个响应项额外调用两次 - 但避免这种情况的唯一方法是更改​​服务以将此信息嵌入列表资源中。

【讨论】:

    猜你喜欢
    • 2019-01-03
    • 2016-07-23
    • 2015-11-13
    • 2015-12-03
    • 1970-01-01
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多