Home > database >  Replace deprecated InvocationRecordingMethodInterceptor
Replace deprecated InvocationRecordingMethodInterceptor

Time:05-24

I have this old hateos code which I would like to migrate to latest Spring hateos version

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());
  }

I can't find how to replace InvocationRecordingMethodInterceptor, LastInvocationAware and MethodInvocation in order to use the latest version of hateos. Can you give me advise how to fix this code?

CodePudding user response:

As described in this Github issue, the structure of packages of Spring HATEOAS was heavily refactored.

In the specific case of DummyInvocationsUtils it was migrated from org.springframework.hateoas.core to the org.springframework.hateoas.server.core package.

For using the new version update your import in consequence:

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

Please, be aware that both LastInvocationAware and MethodInvocation are defined in their own class files, in the same package as DummyInvocationUtils.

Your final code could be like this:

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());
}
  • Related