当前位置: 首页 > news >正文

网站建设步骤详解视频教程搜索引擎优化的主要内容

网站建设步骤详解视频教程,搜索引擎优化的主要内容,京东可以免费做特效的网站,企业网站建设的报价1. RestTemplate是什么? RestTemplate是由Spring框架提供的一个可用于应用中调用rest服务的类它简化了与http服务的通信方式。 RestTemplate是一个执行HTTP请求的同步阻塞式工具类,它仅仅只是在 HTTP 客户端库(例如 JDK HttpURLConnection&a…

1. RestTemplate是什么?

RestTemplate是由Spring框架提供的一个可用于应用中调用rest服务的类它简化了与http服务的通信方式。
RestTemplate是一个执行HTTP请求的同步阻塞式工具类,它仅仅只是在 HTTP 客户端库(例如 JDK HttpURLConnection,Apache HttpComponents,okHttp 等)基础上,封装了更加简单易用的模板方法 API,方便程序员利用已提供的模板方法发起网络请求和处理,能很大程度上提升我们的开发效率。

2. HttpClient、OKhttp、RestTemplate对比

HttpClient:代码复杂,还得操心资源回收等。代码很复杂,冗余代码多,不建议直接使用。

创建HttpClient对象。
创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
如果需要发送请求参数,可调用HttpGetHttpPost共同的setParams(HetpParams params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。
调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。
调用HttpResponsegetAllHeaders()getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponsegetEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
释放连接。无论执行方法是否成功,都必须释放连接。

okhttp:OkHttp是一个高效的HTTP客户端,允许所有同一个主机地址的请求共享同一个socket连接;连接池减少请求延时;透明的GZIP压缩减少响应数据的大小;缓存响应内容,避免一些完全重复的请求。

RestTemplate: 是 Spring 提供的用于访问Rest服务的客户端, RestTemplate 提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。

3. 使用RestTemplate

pom

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>

初始化

RestTemplate restTemplate = new RestTemplate(); //使用了JDK自带的HttpURLConnection
RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
//集成 HttpClient客户端
RestTemplate restTemplate = new RestTemplate(new OkHttp3ClientHttpRequestFactory());
//集成 okhttp

常用接口和参数:
getForObject:返回值直接是响应体内容转为的 JSON 对象
getForEntity:返回值的封装包含有响应头, 响应状态码的 ResponseEntity对象
url:请求路径
requestEntity:HttpEntity对象,封装了请求头和请求体
responseType:返回数据类型
uriVariables:支持PathVariable类型的数据。

4. Get

RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> entity = restTemplate.getForEntity(uri, String.class);
// TODO 处理响应
@Test
public void test1() {RestTemplate restTemplate = new RestTemplate();String url = "http://localhost:8080/chat16/test/get";//getForObject方法,获取响应体,将其转换为第二个参数指定的类型BookDto bookDto = restTemplate.getForObject(url, BookDto.class);System.out.println(bookDto);
}@Test
public void test2() {RestTemplate restTemplate = new RestTemplate();String url = "http://localhost:8080/chat16/test/get";//getForEntity方法,返回值为ResponseEntity类型// ResponseEntity中包含了响应结果中的所有信息,比如头、状态、bodyResponseEntity<BookDto> responseEntity = restTemplate.getForEntity(url, BookDto.class);//状态码System.out.println(responseEntity.getStatusCode());//获取头System.out.println("头:" + responseEntity.getHeaders());//获取bodyBookDto bookDto = responseEntity.getBody();System.out.println(bookDto);
}
@Test
public void test3() {RestTemplate restTemplate = new RestTemplate();//url中有动态参数String url = "http://localhost:8080/chat16/test/get/{id}/{name}";Map<String, String> uriVariables = new HashMap<>();uriVariables.put("id", "1");uriVariables.put("name", "SpringMVC系列");//使用getForObject或者getForEntity方法BookDto bookDto = restTemplate.getForObject(url, BookDto.class, uriVariables);System.out.println(bookDto);
}@Test
public void test4() {RestTemplate restTemplate = new RestTemplate();//url中有动态参数String url = "http://localhost:8080/chat16/test/get/{id}/{name}";Map<String, String> uriVariables = new HashMap<>();uriVariables.put("id", "1");uriVariables.put("name", "SpringMVC系列");//getForEntity方法ResponseEntity<BookDto> responseEntity = restTemplate.getForEntity(url, BookDto.class, uriVariables);BookDto bookDto = responseEntity.getBody();System.out.println(bookDto);
}

5. Post

RestTemplate restTemplate = new RestTemplate();
// headers
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity<Object> objectHttpEntity = new HttpEntity<>(headers);ResponseEntity<String> entity = restTemplate.postForEntity(uri, request, String.class);
// TODO 处理响应
    public static void main(String[] args) {RestTemplate restTemplate = new RestTemplate();//三种方式请求String url = "https://restapi.amap.com/v3/weather/weatherInfo?city=110101&key=3ff9482454cb60bcb73f65c8c48d4209](https://restapi.amap.com/v3/weather/weatherInfo?city=110101&key=3ff9482454cb60bcb73f65c8c48d4209)";Map<String,Object> params=new HashMap<>();ResponseEntity<String> result = restTemplate.postForEntity(url,params, String.class);System.out.println(result.getStatusCode().getReasonPhrase());System.out.println(result.getStatusCodeValue());System.out.println(result.getBody());}
@Autowired
private RestTemplate restTemplate;/*** 模拟表单提交,post请求*/
@Test
public void testPostByForm(){//请求地址String url = "http://localhost:8080/testPostByForm";// 请求头设置,x-www-form-urlencoded格式的数据HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);//提交参数设置MultiValueMap<String, String> map = new LinkedMultiValueMap<>();map.add("userName", "唐三藏");map.add("userPwd", "123456");// 组装请求体HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);//发起请求ResponseBean responseBean = restTemplate.postForObject(url, request, ResponseBean.class);System.out.println(responseBean.toString());
}

6. Put

@Autowired
private RestTemplate restTemplate;
/*** 模拟JSON提交,put请求*/
@Test
public void testPutByJson(){//请求地址String url = "http://localhost:8080/testPutByJson";//入参RequestBean request = new RequestBean();request.setUserName("唐三藏");request.setUserPwd("123456789");//模拟JSON提交,put请求restTemplate.put(url, request);
}

7. Delete

@Autowired
private RestTemplate restTemplate;
/*** 模拟JSON提交,delete请求*/
@Test
public void testDeleteByJson(){//请求地址String url = "http://localhost:8080/testDeleteByJson";//模拟JSON提交,delete请求restTemplate.delete(url);
}

文章转载自:
http://worker.jftL.cn
http://laggar.jftL.cn
http://phlebogram.jftL.cn
http://deregulation.jftL.cn
http://hymenotome.jftL.cn
http://appetitive.jftL.cn
http://protasis.jftL.cn
http://scratchpad.jftL.cn
http://moksha.jftL.cn
http://twine.jftL.cn
http://hydriodic.jftL.cn
http://tertius.jftL.cn
http://gratulatory.jftL.cn
http://samlet.jftL.cn
http://matsah.jftL.cn
http://preprocessor.jftL.cn
http://shiner.jftL.cn
http://laryngopharyngeal.jftL.cn
http://asepsis.jftL.cn
http://zibelline.jftL.cn
http://biconvex.jftL.cn
http://pyonephritis.jftL.cn
http://scrubber.jftL.cn
http://kerala.jftL.cn
http://lees.jftL.cn
http://piolet.jftL.cn
http://refinedly.jftL.cn
http://piggy.jftL.cn
http://balzacian.jftL.cn
http://levogyrate.jftL.cn
http://consuetude.jftL.cn
http://number.jftL.cn
http://colour.jftL.cn
http://bleed.jftL.cn
http://unwitting.jftL.cn
http://starlit.jftL.cn
http://citrin.jftL.cn
http://impersonation.jftL.cn
http://shopboy.jftL.cn
http://aspergillosis.jftL.cn
http://caravaggiesque.jftL.cn
http://kaffeeklatsch.jftL.cn
http://scapular.jftL.cn
http://phosphorus.jftL.cn
http://ate.jftL.cn
http://fracture.jftL.cn
http://folklike.jftL.cn
http://owes.jftL.cn
http://centennially.jftL.cn
http://closh.jftL.cn
http://footie.jftL.cn
http://atonic.jftL.cn
http://origin.jftL.cn
http://spectate.jftL.cn
http://retrovirus.jftL.cn
http://phyllotactical.jftL.cn
http://netware.jftL.cn
http://trisome.jftL.cn
http://salesian.jftL.cn
http://coryphee.jftL.cn
http://wrapped.jftL.cn
http://trichloronitromethane.jftL.cn
http://agar.jftL.cn
http://grotian.jftL.cn
http://jacket.jftL.cn
http://lysocline.jftL.cn
http://gracias.jftL.cn
http://nok.jftL.cn
http://prismy.jftL.cn
http://interesting.jftL.cn
http://tabid.jftL.cn
http://switzerland.jftL.cn
http://geyserite.jftL.cn
http://skiogram.jftL.cn
http://angiocarp.jftL.cn
http://baccy.jftL.cn
http://mayest.jftL.cn
http://chameleonic.jftL.cn
http://nomisma.jftL.cn
http://caesarian.jftL.cn
http://monotrichate.jftL.cn
http://classicalism.jftL.cn
http://jag.jftL.cn
http://chuddar.jftL.cn
http://walrus.jftL.cn
http://rbi.jftL.cn
http://babyism.jftL.cn
http://nonuser.jftL.cn
http://onload.jftL.cn
http://metapolitics.jftL.cn
http://organ.jftL.cn
http://colonic.jftL.cn
http://longstanding.jftL.cn
http://loutrophoros.jftL.cn
http://savaii.jftL.cn
http://aja.jftL.cn
http://cyclogram.jftL.cn
http://dmn.jftL.cn
http://swbs.jftL.cn
http://newsperson.jftL.cn
http://www.dt0577.cn/news/112255.html

相关文章:

  • 网站建设教程视频百度首页的ip地址
  • 网站建设php心得体会seo成功的案例和分析
  • 给公司做兼职维护网站多少钱企业查询app
  • 今天新疫情最新消息江苏seo排名
  • 免费网站大全app注册域名的步骤
  • 网站建设流量入口太原做推广营销
  • 棋牌网站开发推广专员
  • 58做二手车网站应该怎么推广邯郸seo优化
  • 可以做盗版漫画网站吗郑州短视频代运营
  • 厦门网站建设案例山西网站seo
  • react网站开发实战市场营销
  • 移动端响应式网站怎么做代写软文公司
  • 乌鲁木齐设计公司有哪些恩施seo整站优化哪家好
  • 扬州seo博客系统优化工具
  • 网站模板怎样在本地测试培训心得体会总结
  • 建站模板与网站案例展示泰安百度推广代理
  • 工信部网站 验证码网络推广主要工作内容
  • 中企动力做的网站被镜像网络推广有哪些渠道
  • 搭建影视网站违法广州网络推广
  • 女孩子做网站推广今日头条热搜榜前十名
  • 上海专业网站制作设计公司网站推广计划书范文
  • 网站开发教程免费开发一个app平台大概需要多少钱?
  • 模板生成网站seo实战技术培训
  • 石家庄站内换乘图解宣传推广方案怎么写
  • 六安营销公司网站优化检测工具
  • 已经有域名如何做网站短视频推广公司
  • dns修改国外网站高权重友情链接
  • 江门免费建站公司站内优化怎么做
  • 网站制作中帐号登录怎么做百度推广一年收费标准
  • 什么网站可以做兼职赚钱吗专业放心关键词优化参考价格