开发的时候我们可能会和其他公司合作开发一个项目,数据是保存在对方的数据库,调用的也是对方的增删改查方法,我们只需要在controller层处理数据,这时候我们就需要条用别人的接口,以json或xml格式传递数据。这里使用json传递数据。
下面是工具类:
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
* 前台用户对接的请求方法
* @version :2016年4月18日 下午4:39:30
*/
public class HttpSendUtils {
/**
* oauth2GetUrl = PropertyUtils.getPropertyValue(new File(realPathResolver.get(Constants.JEECMS_CONFIG)), OAUTH2_KEY);
* @version :2016年4月18日 下午4:53:25
* @param url
* @param jsonParame 参数以json格式传递
* @throws IOException
*/
public static String sendPost( String url , String jsonParame ) throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
CloseableHttpResponse response = null;;
String html = null;//返回值
try {
// List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
StringEntity entity = new StringEntity(jsonParame ,"UTF-8");
httppost.setEntity(entity);
response = httpclient.execute(httppost);
HttpEntity httpEntity = response.getEntity();
html = EntityUtils.toString(httpEntity, "UTF-8");
} catch (ClientProtocolException e) {
throw new ClientProtocolException(e);
} catch (IOException e) {
throw new IOException(e);
}finally {
response.close();
}
return html;
}
}
springmvc controller层中使用的例子,这里以修改为例
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.fire.back.entity.Config;
import com.fire.back.service.ConfigService;
import com.fire.back.utils.BackUtils;
import com.fire.front.Constants;
import com.fire.front.utils.HttpSendUtils;
import com.fire.pojo.TrExaminationItems;
import com.xxx.common.util.PropertyUtils;
import com.xxx.common.web.springmvc.RealPathResolver;
import com.xxx.core.BaseAction;
/**
* 试题
* @author wsc 2016年4月18日
*/
@Controller
@RequestMapping("/item")
public class ExamItemAct extends BaseAction {
public static final Logger log = LoggerFactory.getLogger(ExamItemAct.class);
private static final String ITEM_ADD = "back.exam.item.add";
private static final String ITEM_LIST = "back.exam.item.list";
private static final String ITEM_EDIT = "back.exam.item.edit";
private static final String ITEM_VIEW = "back.exam.item.view";
@Autowired
private RealPathResolver realPathResolver;
@Autowired
private ConfigService configService;
//去修改页面
@RequestMapping("/edit.do")
public String edit(String uuid,HttpServletRequest request, ModelMap model) {
String message = null;
Config config = configService.findThisConfig();
if(StringUtils.isBlank(uuid)){
message = "该题不存在";
model.addAttribute("message", message);
return BackUtils.returnPage(config, "paper/exam", "list", request, model);
}
try {
// 获取试题信息,这里需要调用对方的查询方法,这是获取action地址的一个方法工具类方法
String url = PropertyUtils.getPropertyValue(new File(realPathResolver.get(Constants.GWOLD_CONFIG)), ITEM_EDIT);
String jsonParame = "{}";//这里以json格式传递参数
String reponse = HttpSendUtils.sendPost(url, jsonParame);
JSONObject jsonObject = new JSONObject(reponse);
String ret_code = jsonObject.getString("ret_code");
//1表示成功
if (StringUtils.isNotBlank(ret_code) && "1".equals(ret_code)) {
String json = jsonObject.getString("data");
net.sf.json.JSONObject dataJson = net.sf.json.JSONObject.fromObject(json);
TrExaminationItems bean = (TrExaminationItems) net.sf.json.JSONObject.toBean(dataJson, TrExaminationItems.class);//将获取的json转换成实体
model.addAttribute("bean", bean);
return BackUtils.returnPage(config, "paper/exam", "edit", request,model);
}
message = "获取试题失败";
model.addAttribute("message", message);
} catch (Exception e) {
message = "获取试题新失败";
log.error(message, e);
}
return BackUtils.returnPage(config, "paper/exam", "list", request,model);
}
@RequestMapping("/update.do")
public String update(TrExaminationItems examinationItems,HttpServletRequest request, ModelMap model) {
String message = null;
Config config = configService.findThisConfig();
if (null == examinationItems) {
message = "提交的内容为空";
}
try {
// 保存试题信息,也是调用对方的方法,这里只是获取action地址的一个方法
String url = PropertyUtils.getPropertyValue(new File(realPathResolver.get(Constants.GWOLD_CONFIG)), ITEM_ADD);
String jsonParame = "{}";
String reponse = HttpSendUtils.sendPost(url, jsonParame);
JSONObject jsonObject = new JSONObject(reponse);
String ret_code = jsonObject.getString("ret_code");
if (StringUtils.isNotBlank(ret_code) && "1".equals(ret_code)) {
message = "更新试题成功";
model.addAttribute("message", message);
return BackUtils.returnPage(config, "paper/exam", "list", request,model);
}
message = "更新试题失败";
model.addAttribute("message", message);
model.addAttribute("examinationItems", examinationItems);
return BackUtils.returnPage(config, "paper/exam", "edit", request,model);
} catch (Exception e) {
message = "更新试题失败";
log.error(message, e);
model.addAttribute("message", message);
model.addAttribute("examinationItems", examinationItems);
return BackUtils.returnPage(config, "paper/exam", "edit", request,model);
}
}
}
下面是上面使用的json的数据格式:
{ "ret_code" : "1",
"ret_msg" : "登录成功",
"data" :[{ "user_uuid" : "9133537D997811E582D7D89D672B59D4",
"person_uuid" : "9133540C997811E582D7D89D672B59D4",
"imgpath" : "http://xxxxxxxxxxxx" }]}