替换已弃用的 InvocationRecordingMethodInterceptor

Replace deprecated InvocationRecordingMethodInterceptor

我有这个旧的 hateos 代码,我想迁移到最新的 Spring hateos 版本

import org.springframework.hateoas.core.DummyInvocationUtils;

public LinkBuilder linkTo(Object dummyInvocation) {
    if (!(dummyInvocation instanceof DummyInvocationUtils.InvocationRecordingMethodInterceptor)) {
      IllegalArgumentException cause =
          new IllegalArgumentException("linkTo(Object) must be call with a dummyInvocation");
      throw InternalErrorException.builder()
          .cause(cause)
          .build();
    }
    DummyInvocationUtils.LastInvocationAware lastInvocationAware =
        (DummyInvocationUtils.LastInvocationAware) dummyInvocation;

    DummyInvocationUtils.MethodInvocation methodInvocation = lastInvocationAware.getLastInvocation();

    StaticPathLinkBuilder staticPathLinkBuilder = getThis();
    return staticPathLinkBuilder.linkTo(methodInvocation.getMethod(), methodInvocation.getArguments());
  }

我找不到如何替换 InvocationRecordingMethodInterceptorLastInvocationAwareMethodInvocation 以使用最新版本的 hateos。你能告诉我如何修复这段代码吗?

this Github issue 中所述,Spring HATEOAS 的包结构进行了大量重构。

DummyInvocationsUtils 的特定情况下,它是 migratedorg.springframework.hateoas.coreorg.springframework.hateoas.server.core 包。

要使用新版本,请更新您的导入:

import org.springframework.hateoas.server.core.DummyInvocationUtils;

请注意,LastInvocationAwareMethodInvocation 都在它们自己的 class 文件中定义,与 DummyInvocationUtils.

在同一个包中

你的最终代码可能是这样的:

import org.springframework.hateoas.server.core.DummyInvocationUtils;
import org.springframework.hateoas.server.core.LastInvocationAware;
import org.springframework.hateoas.server.core.MethodInvocation;

//...

public LinkBuilder linkTo(Object dummyInvocation) {
    if (!(dummyInvocation instanceof LastInvocationAware)) {
      IllegalArgumentException cause =
          new IllegalArgumentException("linkTo(Object) must be call with a dummyInvocation");
      throw InternalErrorException.builder()
          .cause(cause)
          .build();
    }

    LastInvocationAware lastInvocationAware =
        (LastInvocationAware) dummyInvocation;

    MethodInvocation methodInvocation = lastInvocationAware.getLastInvocation();

    StaticPathLinkBuilder staticPathLinkBuilder = getThis();
    return staticPathLinkBuilder.linkTo(methodInvocation.getMethod(), methodInvocation.getArguments());
}