0

I have a rest endpoint like this

@Path("/keys")
public class KeysResource {

     keyService my_keyService = new keyService();
/**
 * Check if the API is alive
 * @return 204 on existing API, else the framework returns 404.
 */
@HEAD
public Response head() {
   return my_keyService.head();
}

And I have the keyService class like this

public Response head() {
   return Response.status(Status.NO_CONTENT).build();

   //I want to have a function like this
    keyInfo.getKeyInfo();
}

So in the service class, I want to have a function to get the request and response information when users calling this API. How can I get the information from the service class??

HenlenLee
  • 435
  • 1
  • 11
  • 30
  • To get what *response* information? You're the one who supposed to generate the response. – Andreas Oct 01 '18 at 21:35
  • What *request* information in particular? It's up to the end-point code to supply the service method with any information it needs, so the service method should have parameter for the values needed, and the end-point method can then receive provide that from any predefined JAX-RS parameters. – Andreas Oct 01 '18 at 21:38
  • 1
    FYI: The service method should not return a `Response` object. The JAX-RS objects should be used only by the end-point handler methods. The service methods should be independent of JAX-RS. – Andreas Oct 01 '18 at 21:40
  • So I can only get the request body and response body in the first KeysResource class, right? – HenlenLee Oct 02 '18 at 01:55
  • That's where you can add parameters for receiving them, yes. – Andreas Oct 02 '18 at 02:32
  • Hi Andreas, I still have some problems. The request information which I want to get is something like userIP, http method and timestamp. How can I get these information in the keyResource class or keyService class? – HenlenLee Oct 02 '18 at 15:10
  • See [second answer](https://stackoverflow.com/a/9627801/5221149) in duplicate. We do provide those links so you can read find your answer there. --- What "timestamp"? HTTP requests don't have timestamps. – Andreas Oct 02 '18 at 15:48

0 Answers0