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

怎么搭建个人网站电脑做服务器seo推广岗位职责

怎么搭建个人网站电脑做服务器,seo推广岗位职责,小程序商城设计,自己搭建聊天平台一、什么是RestTemplate RestTemplate 是Spring框架提供的一个用于应用中调用REST服务的类。它简化了与HTTP服务的通信,统一了RESTFul的标准,并封装了HTTP连接,我们只需要传入URL及其返回值类型即可。RestTemplate的设计原则与许多其他Sprin…

一、什么是RestTemplate

RestTemplate 是Spring框架提供的一个用于应用中调用REST服务的类。它简化了与HTTP服务的通信,统一了RESTFul的标准,并封装了HTTP连接,我们只需要传入URL及其返回值类型即可。RestTemplate的设计原则与许多其他Spring的模板类(如JdbcTemplate)相同,为执行复杂任务提供了一种具有默认行为的简化方法。

二、RestTemplate 的常用方法

RestTemplate提供了多种方法来进行HTTP请求,主要方法包括:

1. GET请求:

  • getForObject(String url, Class responseType, Object… uriVariables): 直接返回响应体中的数据。
  • getForEntity(String url, Class responseType, Object… uriVariables): 返回一个ResponseEntity对象,其中包含了响应的详细信息,如状态码、响应头等。

2. POST请求:

  • postForObject(String url, Object request, Class responseType): 发送POST请求,并返回响应体中的数据。
  • postForEntity(String url, Object request, Class responseType): 发送POST请求,并返回一个ResponseEntity对象。

3. PUT请求:

  • put(String url, Object request): 发送PUT请求。
  • putForObject(String url, Object request, Class responseType): 发送PUT请求,并返回响应体中的数据。

4. DELETE请求:

  • delete(String url): 发送DELETE请求。
    其他方法:
  • exchange(RequestEntity<?> request, Class responseType): 这是一个通用的方法,可以根据RequestEntity对象发送请求,并返回ResponseEntity对象。
    在这里插入图片描述

三、RestTemplate基本使用

1. pom 文件依赖

Spring Boot的 web starter 已经内置了RestTemplate的Bean,我们主需要将它引入到我们的Spring Context中,再进行下简单的配置就可以直接使用了。

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.76</version>
</dependency>

2. 配置 RestTemplate

(1)默认配置:

RestTemplate默认使用 SimpleClientHttpRequestFactory,内部调用JDK的HttpURLConnection进行HTTP请求,默认的超时时间为-1(即无限期等待)。

@Configuration  
public class RestClientConfig {  @Bean  public RestTemplate restTemplate(RestTemplateBuilder builder) {  return builder.build();  }  // 或者自定义请求工厂  @Bean  public RestTemplate restTemplate() {  SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();  factory.setReadTimeout(5000); // 设置读取超时时间  factory.setConnectTimeout(5000); // 设置连接超时时间  return new RestTemplate(factory);  }  
}

(2)自定义配置:

可以通过设置ClientHttpRequestFactory来自定义RestTemplate的配置,如使用HttpComponentsClientHttpRequestFactory(基于Apache HttpClient)或OkHttp3ClientHttpRequestFactory(基于OkHttp)等。
可以配置连接池、超时时间、请求和响应的编解码等。

@Configuration
public class RestTemplateConfig {@Bean public HttpClientConnectionManager poolingHttpClientConnectionManager() {PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();// 设置最大连接数poolingHttpClientConnectionManager.setMaxTotal(500);// 设置每个路由的最大连接数poolingHttpClientConnectionManager.setDefaultMaxPerRoute(100);return poolingHttpClientConnectionManager;}@Beanpublic HttpClient httpClient(HttpClientConnectionManager poolingHttpClientConnectionManager) {HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();// 设置 HttpClient 的连接管理器httpClientBuilder.setConnectionManager(poolingHttpClientConnectionManager);return httpClientBuilder.build();}@Beanpublic ClientHttpRequestFactory clientHttpRequestFactory(HttpClient httpClient) {HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory();// 设置 HttpClientclientHttpRequestFactory.setHttpClient(httpClient);// 设置连接超时时间(毫秒)clientHttpRequestFactory.setConnectTimeout(5 * 1000);// 设置读取超时时间(毫秒)clientHttpRequestFactory.setReadTimeout(10 * 1000);// 设置从连接池获取连接的超时时间(毫秒)clientHttpRequestFactory.setConnectionRequestTimeout(10 * 1000);return clientHttpRequestFactory;}@Beanpublic RestTemplate restTemplate(ClientHttpRequestFactory clientHttpRequestFactory) {RestTemplate restTemplate = new RestTemplate();// 设置请求工厂restTemplate.setRequestFactory(clientHttpRequestFactory);return restTemplate;}}

四、服务端代码实战

1. 服务端接口类

@RestController
public class RestControllerDemo {/*** 普通Get** @param name* @return*/@GetMapping("/get")private String getMethod(@RequestParam("name") String name) {System.out.println("getMethod : name=" + name);return name;}/*** Restful Get** @param name* @return*/@GetMapping("/getName/{name}")private String getRestName(@PathVariable("name") String name) {System.out.println("getRestName : name=" + name);return name;}/*** post** @param name* @return*/@PostMapping("/post")private String postMethod(@RequestParam("name") String name) {System.out.println("postMethod : name=" + name);return name;}/*** post json** @param stu* @return*/@PostMapping("/postBody")public String postBodyMethod(@RequestBody String stu) {Student student = JSONObject.parseObject(stu, Student.class);System.out.println("postBodyMethod : student=" + student);return student.toString();}
}

2. 测试类

@SpringBootTest
class DemoApplicationTests {// 引入 restTemplate@Resourceprivate RestTemplate restTemplate;@Testvoid getTest() {String str = restTemplate.getForObject("http://localhost:8888/get?name=zs", String.class);System.out.println(str);}@Testvoid getRestTest() {String name = "ls";String str = restTemplate.getForObject("http://localhost:8888/getName/" + name, String.class);System.out.println(str);}@Testvoid postTest() {LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>();map.set("name", "zs");String str = restTemplate.postForObject("http://localhost:8888/post", map, String.class);System.out.println(str);}@Testvoid postBodyTest() {HttpHeaders headers = new HttpHeaders();MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");headers.setContentType(type);headers.add("Accept", MediaType.APPLICATION_JSON.toString());HashMap<String, Object> map = new HashMap<>();map.put("name", "zs");map.put("age", 23);String stu = JSON.toJSONString(map);HttpEntity<String> formEntity = new HttpEntity<String>(stu, headers);String str = restTemplate.postForObject("http://localhost:8888/postBody", formEntity, String.class);System.out.println(str);}}

3. exchange 使用示例

通过 HttpHeaders 和 UriComponentsBuilder 可以方便地添加自定义请求头和构建带参数的 URL。

UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)  .queryParam("param1", "value1")  .queryParam("param2", "value2");  String finalUrl = builder.toUriString();  
HttpHeaders headers = new HttpHeaders();  
headers.add("Custom-Header", "HeaderValue");  
HttpEntity<String> request = new HttpEntity<>(null, headers);  ResponseEntity<String> response = restTemplate.exchange(  finalUrl,  HttpMethod.GET,  request,  String.class  
);
http://www.hotlads.com/news/4621.html

相关文章:

  • 湖南城乡建设部网站济南网络推广公司
  • 做网站怎么接业务免费建站免费网站
  • 网站建设大概费用软文接单平台
  • 很多搜索词网站怎样做怎样制作网页
  • 广州宣传片制作公司谷歌优化教程
  • 二次元主题wordpressseo免费优化网站
  • 人防工程做资料的网站网站安全检测工具
  • 网页制作下载什么软件疫情二十条优化措施
  • 杭州seo建站产品怎么在网上推广
  • 药房网站模板推广软文是什么
  • asp.net网站制作视频教程如何做好线上推广
  • 国外优秀电商设计网站网站制作优化
  • 英文外贸网站设计优化大师班级优化大师
  • 重庆慕尚网站建设清远网站seo
  • 苍南最好的网站建设公司优化设计的答案
  • 神华科技 网站建设杭州seo论坛
  • 无线播放电视的浏览器seo推广外包企业
  • 深圳极速网站建设报价网站建设软件
  • 做pcr查基因序列的网站中小企业网站优化
  • 网站目录做跳转建站开发
  • 中卫网站制作公司报价百度电脑版官方下载
  • 网站里的活动专题栏怎么做网站服务器速度对seo有什么影响
  • 国家税务总局网上办税服务厅登录苏州seo建站
  • 购物网站有哪些平台推广计划书怎么写
  • 做四级题目的网站个人网页设计
  • 专业做鞋子的网站吗苏州seo怎么做
  • nba最新消息windows优化大师软件介绍
  • 人力资源网站开发说明书百度官方客户端
  • 青岛网站建设有限公司拼多多seo 优化软件
  • 中国内地服务器连接美国和香港的网站快吗今日国际新闻10条