spring boot 后台访问接口
2019-08-10 17:04
399 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/lhp_wangye/article/details/99088099
现在做一个项目,大概内容是 从别的项目接口里拿数据,
然后利用socket 通讯 跟银行对接, 也获取数据
两个数据比对后返回前台
开始
public PayDO search(StudentDO studentDO) throws IOException { //调用后台接口 String data = JSONObject.toJSONString(studentDO); //要请求到接口的参数--这里是json 格式的字符串 String fileServiceUrl="http://127.0.0.1:8088/school/getone"; //接口路径 //发送请求,获取结果 String result = HttpUtil.doPost(fileServiceUrl, data);//result 就是结果 PayDO entiy = null; if(StringUtils.isNotBlank(result)){ //把json 串转成实体 com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper(); entiy = mapper.readValue(result, PayDO.class); System.out.println(entiy); }
下面是util
import com.yhk.weixin.search.domain.StudentDO; import org.apache.commons.lang.StringUtils; import org.apache.http.HttpEntity; import org.apache.http.client.config.RequestConfig; 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.protocol.HTTP; import org.apache.http.util.EntityUtils; import java.util.HashMap; import java.util.Map; /** * Http工具类 */ public class HttpUtil { /** * 发送POST请求 * @param url 请求url * @param data 请求数据 * @return 结果 */ @SuppressWarnings("deprecation") public static String doPost(String url, String data) { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(10000).setConnectTimeout(20000) .setConnectionRequestTimeout(10000).build(); httpPost.setConfig(requestConfig); String context = StringUtils.EMPTY; if (!StringUtils.isEmpty(data)) { StringEntity body = new StringEntity(data, "utf-8"); httpPost.setEntity(body); } // 设置回调接口接收的消息头 httpPost.addHeader("Content-Type", "application/json"); CloseableHttpResponse response = null; try { response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); context = EntityUtils.toString(entity, HTTP.UTF_8); } catch (Exception e) { e.getStackTrace(); } finally { try { response.close(); httpPost.abort(); httpClient.close(); } catch (Exception e) { e.getStackTrace(); } } return context; } /** * 解析出url参数中的键值对 * @param url url参数 * @return 键值对 */ public static Map<String, String> getRequestParam(String url) { Map<String, String> map = new HashMap<String, String>(); String[] arrSplit = null; // 每个键值为一组 arrSplit = url.split("[&]"); for (String strSplit : arrSplit) { String[] arrSplitEqual = null; arrSplitEqual = strSplit.split("[=]"); // 解析出键值 if (arrSplitEqual.length > 1) { // 正确解析 map.put(arrSplitEqual[0], arrSplitEqual[1]); } else { if (arrSplitEqual[0] != "") { map.put(arrSplitEqual[0], ""); } } } return map; } }
相关文章推荐
- SpringBoot通过jar包方式centos后台部署,如何做到退出控制台,链接访问有效
- springboot web项目后台访问404错误排查
- SpringBoot启动后为何无法正常访问接口
- SpringBoot项目访问任意接口出现401错误的解决方案
- SpringBoot后台接口的写法
- SpringBoot第二讲 利用Spring Data JPA实现数据库的访问(二)_分页和JpaSpecificationExecutor接口介绍
- springboot webservice发布成功 项目其他http接口无法正常访问
- Springboot项目部署到tomcat中访问接口出现404的问题
- springboot 使用过滤器获取response内容保存接口访问日志
- spring boot 中访问 REST 接口
- spring-cloud-starter-feign 组件在spring boot中的应用:访问Oauth2资源服务器的资源接口
- SpringBoot-创建RESTful风格的 http接口访问jpa 来操作数据库
- springboot和redis控制单位时间内同个ip访问同个接口的次数
- 关于Springboot部署到远端服务器tomcat后访问接口出现404的问题
- Spring boot项目 使用ip+port+contextPath进行访问的时候会直接显示出项目中的一些接口信息和相关数据
- 企业级后台接口开发。环境基于spring+springboot+springmvc+mybatis。测试 junit4
- 关于 SpringBoot项目启动成功,接口无法访问且提示404的问题
- spring boot中的拦截器限制用户访问接口次数
- SpringBoot项目访问任意接口跳转到login登录页面
- springboot框架之访问HTML页面与js、css静态文件