i'm trying tie google's firebase messaging platform app, , i'm trying use spring's built in resttemplate rest abstraction simplify it.
i'm trying to:
resttemplate resttemplate = new resttemplate(); resttemplate.getmessageconverters().add(new gsonhttpmessageconverter()); multivaluemap<string, string> headers = new linkedmultivaluemap<>(); headers.add("authorization", "key=" + constants.firebase_server_key); headers.add("content-type", "application/json"); httpentity<firebasepost> entity = new httpentity<>(fbp, headers); uri uri; uri = new uri(firebaseapi); firebaseresponse fbr = resttemplate.postforobject(uri, entity, firebaseresponse.class);
the firebasepost object contains required fields post message api: firebase api - , have verified request entity works posting string.class, response unmarshalled json.
however, trying response marshall directly firebaseresponse object, call postforobject hangs , never returns.
@jsonignoreproperties(ignoreunknown = true) public class firebaseresponse { public integer multicast_id; public integer success; public integer failure; public integer canonical_ids; public firebaseresponse() {} }
i'm having trouble understanding why call never completes. love able have response directly object.
try this:
package yourpackage; import com.fasterxml.jackson.annotation.jsonignoreproperties; @jsonignoreproperties(ignoreunknown = true) public class firebaseresponse { private long multicast_id; private integer success; private integer failure; private object canonical_ids; public firebaseresponse() { } //---- use 1 ---- public boolean is_success() { if (getsuccess() == 1) { return true; } else { return false; } } public long getmulticast_id() { return multicast_id; } public void setmulticast_id(long multicast_id) { this.multicast_id = multicast_id; } public integer getsuccess() { return success; } public void setsuccess(integer success) { this.success = success; } public integer getfailure() { return failure; } public void setfailure(integer failure) { this.failure = failure; } public object getcanonical_ids() { return canonical_ids; } public void setcanonical_ids(object canonical_ids) { this.canonical_ids = canonical_ids; } @override public string tostring() { return "firebaseresponse{" + "multicast_id=" + multicast_id + ", success=" + success + ", failure=" + failure + ", canonical_ids=" + canonical_ids + '}'; } } //--------------- usage ------------------ arraylist<clienthttprequestinterceptor> interceptors = new arraylist<>(); interceptors.add(new headerrequestinterceptor("authorization", "key=" + firebase_server_key)); interceptors.add(new headerrequestinterceptor("content-type", "application/json")); resttemplate.setinterceptors(interceptors); jsonobject body = new jsonobject(); // jsonarray registration_ids = new jsonarray(); // body.put("registration_ids", registration_ids); body.put("to", "cfw930czxxxxxxxxxxxxxxxxxxxxxxxxxxipdo-bjhlachrqqzc0asxlrfkdmhv_anbxkrzlnxxxxxxxxxxx59spw4rw-5mtwkkzxxxxxxxgxll-llijuujpwzplglpji_"); body.put("priority", "high"); // body.put("dry_run", true); jsonobject notification = new jsonobject(); notification.put("body", "body string here"); notification.put("title", "title string here"); // notification.put("icon", "myicon"); jsonobject data = new jsonobject(); data.put("key1", "value1"); data.put("key2", "value2"); body.put("notification", notification); body.put("data", data); httpentity<string> request = new httpentity<>(body.tostring()); firebaseresponse firebaseresponse = resttemplate.postforobject("https://fcm.googleapis.com/fcm/send", request, firebaseresponse.class); log.info("response is: " + firebaseresponse.tostring()); return new responseentity<>(firebaseresponse.tostring(), httpstatus.ok); //--------------- helper class ------------------ import org.springframework.http.httprequest; import org.springframework.http.client.clienthttprequestexecution; import org.springframework.http.client.clienthttprequestinterceptor; import org.springframework.http.client.clienthttpresponse; import org.springframework.http.client.support.httprequestwrapper; import java.io.ioexception; public class headerrequestinterceptor implements clienthttprequestinterceptor { private final string headername; private final string headervalue; public headerrequestinterceptor(string headername, string headervalue) { this.headername = headername; this.headervalue = headervalue; } @override public clienthttpresponse intercept(httprequest request, byte[] body, clienthttprequestexecution execution) throws ioexception { httprequest wrapper = new httprequestwrapper(request); wrapper.getheaders().set(headername, headervalue); return execution.execute(wrapper, body); } }
Comments
Post a Comment