[英]Spring boot - restTemplate.postForObject - params are null
[英]Spring Boot RestTemplate.postForObject to Firebase Messaging not returning
我正在嘗試將 Google 的 Firebase Messaging 平台綁定到我的應用程序中,並且我正在嘗試使用 Spring 內置的 RestTemplate REST 抽象來簡化它。
我目前正在嘗試:
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);
FireBasePost 對象僅包含 POST 消息 API 所需的字段: Firebase API - 我已經通過使用 String.class 發布來驗證請求實體是否有效,因此響應是未編組的 JSON。
但是,在嘗試將響應直接編組到 FireBaseResponse 對象中時,對 postForObject 的調用掛起並且永遠不會返回。
@JsonIgnoreProperties(ignoreUnknown = true)
public class FireBaseResponse {
public Integer multicast_id;
public Integer success;
public Integer failure;
public Integer canonical_ids;
public FireBaseResponse() {}
}
我無法理解為什么這個調用永遠不會完成。 我希望能夠將響應直接放入一個對象中。
試試這樣:
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 this one ----
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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.