Molay
Molay

Reputation: 1404

RestTemplate not carrying request attributes

i have 2 services, example Parent and Child. In Parent service, have

request.setAttribute("key","someValue");

from Parent Service have invoked one of the endpoints of Child service.

restTemplate.exchange(url, HttpMethod.GET, null, Object.class);

in Child RestController, i am expecting the attributes set (key) in Parent.

request.getAttribute("key") --> returns null

But i am getting null, can you please suggest what i am doing wrong. I was expecting the same request object of Parent would be passed on to Child. But my understanding seems to be wrong. Please correct me.

thanks.

Upvotes: 0

Views: 1582

Answers (1)

artemisian
artemisian

Reputation: 3106

I'm not sure what you are trying to accomplish but it seems your are trying to set an attribute in your HttpServletRequest and expect to get that attribute back at your endpoint.

The request object is created by the web container but it's not 'transferred' to your Child endpoint as I assume you think it does. That object only exist on the endpoint that is processing the request until a response is sent back.

If you need to pass a param to your Child endpoint you'll need to add it to the url as a query param like:

url += "?key=someValue"

or if its a more complex object you should use a different HTTP mehtod other than GET and add it as the body of your request. Then you should receive the param in the Child endpoint.

You can check several examples here:

https://springbootdev.com/2017/11/21/spring-resttemplate-exchange-method/

Upvotes: 1

Related Questions