Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.mc.printer.model.layout.ws; import com.mc.printer.model.constants.ClientConstants; import com.mc.printer.model.layout.BaseTask; import org.slf4j.LoggerFactory; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpEntity; import org.springframework.http.HttpMethod; import org.springframework.web.client.RestClientException; /** * * @author 305027939 * @param <T> */ public abstract class CommUpdateTask<T> extends BaseTask { private static org.slf4j.Logger logger = LoggerFactory.getLogger(CommUpdateTask.class); private final String URI = ClientConstants.WS_HTTP; private final T requestData; private final String serviceId; public CommUpdateTask(T requestData, String serviceId) { this.requestData = requestData; this.serviceId = serviceId; } @Override public Object doBackgrounp() { ComResponse<T> response = null; try { //Spring3 RESTful client?POSThttp? response = restTemplate.exchange(URI + serviceId, HttpMethod.POST, new HttpEntity<T>(requestData), new ParameterizedTypeReference<ComResponse<T>>() { }).getBody(); } catch (RestClientException e) { //logger.error(e.getMessage()); return e; } return response; } @Override public void onSucceeded(Object object) { if (object == null) { logger.error("response data is null"); failHandle("response data is null"); return; } if (object instanceof Exception) { Exception e = (Exception) object; logger.error(e.getMessage()); failHandle(e.getMessage()); return; } if (object instanceof ComResponse) { responseResult((ComResponse<T>) object); } else { logger.error("response data is not a valid object"); failHandle("response data is not a valid object"); } } public abstract void responseResult(ComResponse<T> response); public void failHandle(String message) { }; }