分布式——接口调用

来源:互联网 发布:Windows的tmp路径 编辑:程序博客网 时间:2024/05/29 12:04
public class Demo {@Autowiredprivate ApiService apiService;private static final ObjectMapper OBJECTMAPPER = new ObjectMapper();@SuppressWarnings("unchecked")@Overridepublic HashMap<String, Object> insertapplyinfo(HashMap<String, Object> map) {HttpResult result=apiService.doPut(http://192.168.0.1:80/rest/popularize/insertByCountLessThanThree", map);if(result.getCode().equals(200)){return OBJECTMAPPER.readValue(result.getBody(),HashMap.class);}} catch (IOException e) {e.printStackTrace();}return null;}}
import java.io.IOException;import java.net.URISyntaxException;import java.util.ArrayList;import java.util.List;import java.util.Map;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.config.RequestConfig;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpDelete;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.client.methods.HttpPut;import org.apache.http.client.utils.URIBuilder;import org.apache.http.entity.ContentType;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;/** * 负责和外部接口对接,发起http请求 */@Servicepublic class ApiService {    @Autowired(required = false)//使用到时才注册该对象    private CloseableHttpClient httpClient;    @Autowired(required = false)//使用到时才注册该对象    private RequestConfig requestConfig;    /**     * 执行DoGET请求     *      * @param url     * @return 如果响应是200,返回具体的响应内容,其他响应返回null     * @throws ClientProtocolException     * @throws IOException     */    public String doGet(String url) throws ClientProtocolException, IOException {        // 创建http GET请求        HttpGet httpGet = new HttpGet(url);        httpGet.setConfig(this.requestConfig);        CloseableHttpResponse response = null;        try {            // 执行请求            response = httpClient.execute(httpGet);            // 判断返回状态是否为200            if (response.getStatusLine().getStatusCode() == 200) {                return EntityUtils.toString(response.getEntity(), "UTF-8");            }        } finally {            if (response != null) {                response.close();            }        }        return null;    }    /**     * 带有参数的GET请求     *      * @param url     * @param param     * @return     * @throws ClientProtocolException     * @throws IOException     * @throws URISyntaxException     */    public String doGet(String url, Map<String, Object> param) throws ClientProtocolException, IOException,            URISyntaxException {        // 定义请求的参数        URIBuilder uriBuilder = new URIBuilder(url);        for (Map.Entry<String, Object> entry : param.entrySet()) {            uriBuilder.addParameter(entry.getKey(), entry.getValue().toString());        }        return doGet(uriBuilder.build().toString());    }    /**     * 指定POST请求     *      * @param url     * @param param 请求参数     * @return 状态码和请求的body     * @throws IOException     */    public HttpResult doPost(String url, Map<String, Object> param) throws IOException {        // 创建http POST请求        HttpPost httpPost = new HttpPost(url);        httpPost.setConfig(this.requestConfig);        if (param != null) {            // 设置post参数            List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);            for (Map.Entry<String, Object> entry : param.entrySet()) {                parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));            }            // 构造一个form表单式的实体,并且指定参数的编码为UTF-8            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters, "UTF-8");            // 将请求实体设置到httpPost对象中            httpPost.setEntity(formEntity);        }        CloseableHttpResponse response = null;        try {            // 执行请求            response = httpClient.execute(httpPost);            if (response.getEntity() != null) {                return new HttpResult(response.getStatusLine().getStatusCode(), EntityUtils.toString(                        response.getEntity(), "UTF-8"));            }            return new HttpResult(response.getStatusLine().getStatusCode(), null);        } finally {            if (response != null) {                response.close();            }        }    }    /**     * 执行PUT请求     *      * @param url     * @param param 请求参数     * @return 状态码和请求的body     * @throws IOException     */    public HttpResult doPut(String url, Map<String, Object> param) throws IOException {        // 创建http POST请求        HttpPut httpPut = new HttpPut(url);        httpPut.setConfig(this.requestConfig);        if (param != null) {            // 设置post参数            List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);            for (Map.Entry<String, Object> entry : param.entrySet()) {                parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));            }            // 构造一个form表单式的实体,并且指定参数的编码为UTF-8            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters, "UTF-8");            // 将请求实体设置到httpPost对象中            httpPut.setEntity(formEntity);        }        CloseableHttpResponse response = null;        try {            // 执行请求            response = httpClient.execute(httpPut);            if (response.getEntity() != null) {                return new HttpResult(response.getStatusLine().getStatusCode(), EntityUtils.toString(                        response.getEntity(), "UTF-8"));            }            return new HttpResult(response.getStatusLine().getStatusCode(), null);        } finally {            if (response != null) {                response.close();            }        }    }    /**     * 指定POST请求     *      * @param url     * @param param 请求参数     * @return 状态码和请求的body     * @throws IOException     */    public HttpResult doPostJson(String url, String json) throws IOException {        // 创建http POST请求        HttpPost httpPost = new HttpPost(url);        httpPost.setConfig(this.requestConfig);        if (json != null) {            // 构造一个字符串的实体            StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);            // 将请求实体设置到httpPost对象中            httpPost.setEntity(stringEntity);        }        CloseableHttpResponse response = null;        try {            // 执行请求            response = httpClient.execute(httpPost);            if (response.getEntity() != null) {                return new HttpResult(response.getStatusLine().getStatusCode(), EntityUtils.toString(                        response.getEntity(), "UTF-8"));            }            return new HttpResult(response.getStatusLine().getStatusCode(), null);        } finally {            if (response != null) {                response.close();            }        }    }    /**     * 没有参数的post请求     *      * @param url     * @return     * @throws IOException     */    public HttpResult doPost(String url) throws IOException {        return doPost(url, null);    }    /**     * 执行PUT请求     *      * @param url     * @return 状态码和请求的body     * @throws IOException     */    public HttpResult doPut(String url) throws IOException {        return this.doPut(url, null);    }    /**     * 执行DELETE请求,通过POST提交,_method指定真正的请求方法     *      * @param url     * @param param 请求参数     * @return 状态码和请求的body     * @throws IOException     */    public HttpResult doDelete(String url, Map<String, Object> param) throws IOException {        param.put("_method", "DELETE");        return this.doPost(url, param);    }    /**     * 执行DELETE请求(真正的DELETE请求)     *      * @param url     * @return 状态码和请求的body     * @throws IOException     */    public HttpResult doDelete(String url) throws IOException {        // 创建http DELETE请求        HttpDelete httpDelete = new HttpDelete(url);        httpDelete.setConfig(this.requestConfig);        CloseableHttpResponse response = null;        try {            // 执行请求            response = httpClient.execute(httpDelete);            if (response.getEntity() != null) {                return new HttpResult(response.getStatusLine().getStatusCode(), EntityUtils.toString(                        response.getEntity(), "UTF-8"));            }            return new HttpResult(response.getStatusLine().getStatusCode(), null);        } finally {            if (response != null) {                response.close();            }        }    }}
@Controller@RequestMapping("popularize")public class PopularizeController {/** * 查询今天这个号码申请的次数 ,如果>=3次,则申请失败,否则申请成功,保存申请信息 * @param url前台js访问后台的url * @param applyinfo * @returnJSON格式的HashMap<String,Object> */ @RequestMapping("insertByCountLessThanThree") @ResponseBody    public ResponseEntity<HashMap<String,Object>> save(Applyinfo applyinfo){     HashMap<String,Object> map = new HashMap<String,Object>();SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式String result = "";        try {        /* 查询今天这个号码申请的次数 */        int count = applyinfoService.queryCountByPhoneAndDate(applyinfo.getCELLPHONE(),df.format(new Date()));        if(count>=3){        result = "fail";        }else{        applyinfoService.insertApplyinfo(applyinfo);        result = "success";        }} catch (Exception e) {e.printStackTrace();result = "error";}        map.put("result", result);        if (StringUtils.isEmpty(map)) {return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);}else{return ResponseEntity.status(HttpStatus.OK).body(map);}    }


                                             
0 0