直接上代码:
package weaver.interfaces.workflow.action;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
public class DenyAction implements Action {
private Log log = LogFactory.getLog(DenyAction.class);
@Override
public String execute(RequestInfo requestInfo) {
// 获取主表数据
Property[] properties = requestInfo.getMainTableInfo().getProperty();
Map<String, Object> mainDatas = new HashMap<>();
for (Property property : properties) {
mainDatas.put(property.getName(), property.getValue());
}
// 构建请求必须的map
// String xmbhValue = (String) mainDatas.get("xmbh");
// Map<String, Object> requestMap = new HashMap<>();
// requestMap.put("xmbh", xmbhValue);
String dataJson = JSON.toJSONString(mainDatas);
RestTemplate restTemplate = new RestTemplate();
String url = "http://192.168.6.112/abc/api/insert";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity entity = new HttpEntity<Object>(dataJson, headers);
ResponseEntity<JSONObject> res = restTemplate.exchange(URI.create(url), HttpMethod.POST, entity, JSONObject.class);
// statusCode错误处理
String statusCode = res.getStatusCode().toString();
if (res.getStatusCodeValue() != 200) {
requestInfo.getRequestManager().setMessageid("-2");
requestInfo.getRequestManager().setMessagecontent("接口状态:" + statusCode + "系统调用外部接口失败!");
log.error("statusCode" + statusCode + "content:" + requestInfo.getRequestManager().getMessagecontent());
return Action.FAILURE_AND_CONTINUE;
}
JSONObject resBody = res.getBody();
if (resBody != null) {
if (resBody.getInteger("state").equals(207) || resBody.getInteger("state").equals(500)) {
//http调用成功,系统执行失败,返回错误信息,并进行异常提示
requestInfo.getRequestManager().setMessageid("-2");
requestInfo.getRequestManager().setMessagecontent("接口状态:" + resBody.getInteger("state") + "系统调用外部接口成功,外部接口执行失败!");
return FAILURE_AND_CONTINUE;
} else {
//系统调用成功,外部系统执行成功
return SUCCESS;
}
}
return SUCCESS;
}
}