-1

I'm working on a springboot project where I need to intercept outgoing http requests and do something with it. I know we can use ClientHttpRequestInterceptor for the same and it works fine too.

But I need to know the java class type of the request body that is being sent in the http POST/PUT request. Currently this interceptor only provides a byte array representation of the request object. Doc here

Is there any way I can get to know the java class type of request object/body? Or is there any other better way to do this other than using the ClientHttpRequestInterceptor?

Updated code sample:

public class MyInterceptor implements ClientHttpRequestInterceptor {


@Override
public ClientHttpResponse intercept(HttpRequest request,
                             byte[] body,
                             ClientHttpRequestExecution execution)
                      throws IOException {

// Do something before service invocation

// I need to get request body's class type but it is just a byte[] here. And the 'HttpRequest' argument doesn't carry any info related to request class type.

ClientHttpResponse clientHttpResponse = execution.execute(request, body);

// Do something after service invocation    
  }
}
Ram
  • 191
  • 1
  • 2
  • 12

2 Answers2

0

byte[] is serialized form of your request Object. Just by having byte[] chunk in hand will not help you in determining what kind of class's Object it was. You need to do some thing more -

If you are implementing your ClientHttpRequestInterceptor for your RestTemplate and you have to do some thing with your Request Object - then you must have to convert byte[] body to your object :

How - Java: object to byte[] and byte[] to object converter (for Tokyo Cabinet)

Once you have an object in hand then use instanceof to check if converted Object belongs to your class:

if(obj instanceof Your_Class){
   // your class specific code ..
}
atul sachan
  • 597
  • 5
  • 10
-1

Of course you can, the recommended way of doing so; is using ِAspect Oriented Programming AOP, which will support you with all details you need about the JointPoint you target; in your case, it will be the method called inside the controller.

Check this example Spring Boot AOP, and also it is better to get your hand dirty with these classes ProceedingJoinPoint and JointPoint

To make it simple, you can use this JointPoint class to get the method signature and of course its parameters, for each parameter you have the type also (which you are looking for), moreover you can use the parameter itself to do whatever logic you need. check the following

@Aspect
@Component
public class SomeAspect {

    @Around("@annotation(SomeAnnotation)")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        CodeSignature codeSignature = (CodeSignature) joinPoint.getSignature();

        System.out.println("Parameters' Types: " + codeSignature.getParameterTypes);
        System.out.println("Parameters' name: " + codeSignature.getParameterNames()[0]);
        System.out.println("Arguments' value: " + joinPoint.getArgs());

        return joinPoint.proceed();
    }
}

The other way which may help you in your code is by using pure reflection and custom annotation, in that way you can get also the all details from Class of your request, but it may require some more code.

Mohamed Sweelam
  • 1,109
  • 8
  • 22