How can I log the raw rest request/response as seen by Jersey? - jersey

I have a Jersey-based REST service deployed on apache tomcat. I would like to log the raw requests / responses made to the survey. The data that I am looking for is the URL and the body (in the case of POST/PUT messages).
How can I enable this logging?

Related

How to store request data and reponse data into database in spring boot application

I am working on to store request data and response data if there any exceptions encountered while sending response back to client I need to store the exception also.
My DB table will ID,RequestData,ResponseData,Exception,TimeTakenToRespond
lets say I have endpoint called /athenticateUser so my input data would be
RequestData :{"username":"mate","password":"swamy"}
ResponseData :{"FirstName":"mate","LastName":"swamy","Email":"manteswamy#gmail.com"}
Like the above way I need to store all the request data and response data if any exception while sending response back to client that also we need store and the web service time taken respond.
As I am beginner to spring boot please guide step by step
Though you can write Interceptors, Filters etc for this. But don't do it.
Please try to use Spring Boot feature Actuator for tracing. It provides HTTP request logging out of the box.
There's an endpoint mapped to /trace or /actuator/httptrace which will show you last 100 HTTP requests. You can customize it to log each request, or write to a DB.
You will need spring-boot-starter-actuator dependency.
You can also whitelist endpoints.
For the guide step by step, you can check this actuator tutorial.
You can do it by using spring boot actuator httptrace..
add below dependency:
implementation('org.springframework.boot:spring-boot-starter-actuator')
Look at this step by step guide..
The Spring-boot actuator doesn't support getting the request/response bodies, they even removed some mechanisms to do it.
Here's a blog post on how to do it with a Filter.

Spring Reactive Rest Log Complete Request Response Body

We have a requirement to log the complete request and response to our JSON based REST endpoints. We are going to use Spring Reactive. Is there a recommended solution that works out of the box? If not, what's the best approach to log request and response?

How to Log all Incoming Request in Spring REST Services

Hi I am using Spring framework to build REST full web services . I want to log all the incoming request to my service. Is there any way I can log all the incoming request to my web service.
You could use Filter or Interceptor if you need very basic solution. Just inject your service into it, and override "preHandle(HttpServletRequest request..."
Problems will come when you decide that you need to:
log only some requests (E.g. only PUT and POST)
exclude some data (e.g. passwords, keys, etc)
exclude files
parse data to extract information to persist it
know if it return success or failure or exception
Then better solution would be to call that service directly from controllers.

Forward Spring HTTP Request

I have a restful web service written using Spring WebMVC that will mostly be used to orchestrate other services. In some cases these services are on the same server, in some cases they are not. I have a few requests (GET and POST) that will be direct pass throughs to another service. Is there a way to blindly forward all GET and POST data from a request for certain URLs without knowing anything about the data in the request?
Ideally, I would like to be able to say all requests for http://server1/myService/user/... should forward to http://server2/user/... with all of the GET and POST parameters forwarded with it.
For the services on the same server, if they're being served by the same Spring MVC application, you could use RedirectViews and/or the "redirect:" prefix.
For those on another server, the best thing I can think of would be to use a servlet filter, similar to the approach suggested by this post: spring mvc redirect path and all children to another domain

custom HTTP status code with spring and restful webservice

I am new to the spring and restful framework. My server hosts 3 restful web services. I have used spring support for the restful web services to implement the server. The client sends JSON request to the server and gets JSON response. Client is based on spring support for the restful. The server returns 200/OK for successful processing, which is default.
What I want is the server to send custom HTTP status code 550 to the client in case there is an issue while processing the request. It should not throw any exception to the client. It should only send 550/an_error_object(as json) back to the client.
How is it possible with the spring support of restful ? I am doing the following on the server side.
HttpServletResponse.setStatus(550)
but, the client side is failing to recognize the status code 550; it throws an exception since org.springframework.http.HttpStatus doesn't have any enum constant '550' defined.
Any suggestions will be great!
Thanks in advance.
Rohit
1) Don't use unregistered status codes. See the registered status codes.
2) That being said, that's a bug in the Spring framework. It should deal properly with extension status codes.
It'll probably going to be fixed in version 4.3 of Spring.
https://jira.spring.io/browse/SPR-14205

Resources