java - Adding data to a Response object in Jersey -


i trying use aspect add timestamp javax.ws.rs.core.response using around advice.

i'm new java , jersey , i'm struggling this. closest have this:

object output = proceed(); method method = ((methodsignature) thisjoinpoint.getsignature()).getmethod(); type type = method.getgenericreturntype();  if (type == response.class) {     system.out.println("we have response!");     response original = (response) output;     output = (object)response.ok(original.getentity(string.class).tostring()+ " " + double.tostring(duration)).build(); }  return output; 

the kind of response produced application/json. want add field json says time:<val of duration>.

the easiest solution make entity classes extend interface has method gettime() , settime() , can set time value in interceptor shown below.

public interface timedentity {     long gettime();      void settime(long time); } 

your actual entity

public class entity implements timedentity {     private long time;      // other fields, getters , setters here..      @override     public long gettime() {         return time;     }      @override     public void settime(long time) {         this.time = time;     } } 

and interceptor

object output = proceed(); method method = ((methodsignature)thisjoinpoint.getsignature()).getmethod(); type type = method.getgenericreturntype();  if (type == response.class) {   system.out.println("we have response!");   response original = (response) output;   if (original != null && original.getentity() instanceof timedentity) {     timedentity timedentity = (timedentity) original.getentity();     timedentity.settime(duration);   }  }else if (output instanceof timedentity) {     timedentity timedentity = (timedentity) output;     timedentity.settime(duration); }  return output; 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -