I am using Hystrix Feign with Spring Encoder.
I want to log exact request (Json) payload that goes out with each request
and also the response.
How can I do that?
When you define your feign configuration, you have to set the feign logger level, according to the feign logger class, you have 4 possible values, so probably you want to use Logger.Level.FULL.
Hope this help you.
Related
I want to route requests based on some values in requestBody in spring cloud, for example:
if value of firstField in requestBody is chagre, I want to route this request to /chagre api
else if value of firstField in requestBody is package, I want to route this request to /package api
Any help would be appreciated.
Best practice is not to route according to request body, but use different attributes of the HTTP request instead. Spring Cloud Gateway includes many built-in route predicate factories based on those attributes.
The problem is request body can be read only once. moreover you need to know the object class it contains in order to properly read it.
In order to construct a solution to your question, we can use Spring Cloud Gateway ModifyRequestBody to rewrite the request body after you read it and before it send to the downstream.
Read more about ModifyRequestBody
For communication we will send encrypted messages between our app and backend. These messages contains encrypted payload which will be json. What is the best way to automatically decrypt the payload and pass it as unmarshalled object to the Spring rest controller. Should I utilize some Spring custom editors?
UPDATE
The problem is quite complicated as we need to use HSM and DB for decryption. I know I can handle this in filter, but I guess the approach is not really good one.
Replacing the request content seems odd to me not to mention the need for starting DB transaction.
Spring interceptors will not help as they are just alternative to filters. We thought about AOP or some Facade before each service call, which fill take care about the messege decryption and unmarshalling.
This seems like a job for spring filters. Filters can intercept your http requests. You can configure filter bean by adding component implementing javax.servlet.Filter interface.
#Component
public class EncodingFilter implements Filter {
}
You can read more on this matter based on example here and here.
My requirement is to call siebel soap webservice, In the process handle request and response on a same method call, so that I can add token to the request header from the apache common pool and once get the response with token, grab the token from response and send it to pool. Here I have mechanism to verify old token too,
I need request token and response token on same class.
Future planning to add retry mechanism.
Currently I am using SI Http outbound gateway.
Any thoughts, appreciate it.
Thanks
So, what you need is named pre- and post-process. Not sure why you don't use Spring Integration WS support for calling that Siegel service, but even with the HTTP you can get a gain via Interceptor abstraction.
What I mean that you can inject RestTemplate into HTTP Outbound Gateway supplied with the ClientHttpRequestInterceptor implementation to provide a desired logic.
If you'd use WS Outbound Gateway, you could do that in the similar ClientInterceptor abstraction.
Of course, you can achieve that via HeaderMapper implementation, but that has different responsibility...
I found the way to achieve this,
Created a class to extends HttpRequestExecutingMessageHandler than overrided handleRequestMessage()
http://docs.spring.io/spring-integration/reference/html/http.html#http-outbound
I have used Spring Amqp Outbound Gateway integration to send request to a third party web service. Below shown is my gateway interface.
public interface AccountManagerGateway {
public RetrieveAccountResponse retrieveAccount(RetrieveAccountRequest request);
}
I need to know how to send custom Message Headers with the gateway call.
Ex:- "AccountID" in the header
I did some google searches but couldn't find a solution. May be I'm doing the search in a wrong context or a wrong direction. I'm expecting your kind support on this.
Please let me know if you need more info. I didn't post my integration-context xml in here because then the post will get lengthy.
Thanks.
See the documentation about gateways.
For example:
public RetrieveAccountResponse retrieveAccount(RetrieveAccountRequest request,
#Header("AccountId") String accountId);
By default, user-defined headers are not sent over AMQP so you need to configure the mapped-request-headers on the outbound gateway; something like
mapped-request-headers="STANDARD_REQUEST_HEADERS,AccountId"
Again, refer to the documentation.
I have a Spring MVC REST service that uses XStream to convert the messages to and from XML.
Is there any way I can print the xml (ie the body) from the request and response to a normal log4j logger?
Inside the controller won't work cause by then XStream has alread unmarshalled the request and not yet marshalled the response.
A filter in the servlet isn't very nice either as it will consume the body by reading it.
And thats where I run out if ideas. So, SO? Any takers? :)
You can use an interceptor to log your request and response content coming to your specific rest uri's - More details here