首页 > 编程知识 正文

前端调用restful接口(java restful接口开发)

时间:2023-05-03 20:23:30 阅读:68195 作者:424

一. pom文件需要使用RestTemplate引入依赖关系

从属关系

groupid org.Apache.http components/groupid

artifactidhttpclient/artifact id

版本4.5.2 /版本

/从属

2 .类的实例化

importorg.spring framework.beans.factory.annotation.auto wired; importorg.spring framework.boot.web.client.resttemplatebuilder; importorg.spring帧work.context.annotation.bean; importorg.spring framework.context.annotation.configuration; importorg.spring framework.http.client.httpcomponentsclienthttprequestfactory; importorg.spring framework.web.client.rest template; @ configurationpublicclassresttempleconfig {//启动时需要注意。 由于控制器中注入了RestTemplate,因此在启动时必须实例化该类的实例@ autowiredprivaterestteter//resttemplatebuilder spring默认情况下填充了RestTemplateBuilder实例@ beanpublicresttemplateresttemplate ({ return builder } ) @ beanpublicresttemplatecustomresttemplate ({ httpcomponentsclienthttprequestfactoryhttprequestfactory=newhttpcomponentttttottory http请求工厂. setconnecttimeout (3000; htprequestfactory.setreadtimeout (3000; returnnewresttemplate (http请求工厂; }三.调用第三方http接口

importorg.spring framework.context.annotation.configuration; importorg.spring帧. scheduling.annotation.enable scheduling; importorg.spring framework.web.client.rest template; importorg.spring framework.beans.factory.annotation.auto wired; @ configuration @ enableschedulingpublicclasstesttask { @ autowiredprivateresttemplateresttemplate; 公共void test () { JSONObject json=new JSONObject; Jon.put('id ',' id ' ); //如果不默认处理,这里返回的是ListLinkedHashMapK,v形式的数据不是我们需要的; mapresult=rest template.postforobject (http :127.0.0.1/test ),json,Map.class ); (四)调用第三方https接口

importorg.spring framework.context.annotation.configuration; importorg.spring帧. scheduling.annotation.enable scheduling; importorg.spring framework.web.client.rest template; importorg.spring framework.beans.factory.annotation.auto wired; @ rest controller @ request mapping (' test ) )公共类测试{ @ autowiredprivateresttemplaterestttemplate; 公共语音测试((resttemplateresttemplate=newresttemplate ) newhttpsclientrequestfactory ) ); string URL=' https://API.weixin.QQ.com '; responseentitystringres=rest template.getforentity (URL,String.class ); jsonobjectjsonobject=JSON object.parse object (RES.getbody ) ); } importorg.Apache.http.conn.SSL.sslcontext s; importorg.Apache.http.conn.SSL.trust strategy; importorg.spring framework.http.client.simpleclienthttprequestfactory; import javax.net.SSL.httpsurlconnection; import javax.net.ssl.SSLContext; import Java.net.httpurl connection; import java.security.KeyStore; 导入Java.security.cert.certificate exception; import Java.security.cert.x509 certificate;/* * @描述: * @ author : bgq * @ create :2021/3/2217336014 */publicclasshttpsclientrequestfactoryextendssimpleclienthttprequestfactory { @ overrideprotectedvoidprepareconnection (连接器tion ) ({//http协议//thrownewruntimeexception ) (aninstanceofhttpsurlconnectionisexpeption ) (aninstanceofhtpsurlcoxpeption ) }连接安装程序(if ) {//https协议,修改协议版本keystore trust store=keystore.getinstance (keystore.getinstance ) 任何链接truststrategyanytruststrategy=newtruststrategy (({ @ overridepublicbooleanistrusted ) x509 certificate [ ] x509 certiced ; slcontextctx=sslcontext s.custom (.use TLS ).loadtrustmaterial ) truststore,anyTrustStrategy ).build ); (HttpsURLConnection (连接).setsslsocketfactory (CTX.getsocketfactory ) ); httpsurlconnectionhttpsconnection=(httpsurlconnection ) connection; super.prepare connection (https connection,httpMethod ); }catch(exceptione ) { e.printStackTrace ); } }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。