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

专门做网络的公司深度优化

专门做网络的公司,深度优化,创立网站做电商,网站上的图文介绍怎么做目录 一、ApiClient代码解读二、ApiService代码解读三、HomeController代码解读四、整体代码五、结果展示 一、ApiClient代码解读 这是一个简单的Spring Boot的RestTemplate客户端,用于执行HTTP请求。 首先,这个类被Component注解标记,这意味…

目录

  • 一、ApiClient代码解读
  • 二、ApiService代码解读
  • 三、HomeController代码解读
  • 四、整体代码
  • 五、结果展示

一、ApiClient代码解读

这是一个简单的Spring Boot的RestTemplate客户端,用于执行HTTP请求。
首先,这个类被@Component注解标记,这意味着它是一个Spring组件,Spring的依赖注入框架会自动创建并管理这个类的实例。

类声明

public class ApiClient {

成员变量

private final RestTemplate restTemplate;

RestTemplate是Spring提供的一个HTTP客户端,用于发送HTTP请求。final`关键字表示这个成员变量一旦在构造函数中被赋值后,就不能再被修改。

无参构造函数

public ApiClient() {restTemplate = new RestTemplate();
}

这个无参构造函数是创建一个新的ApiClient实例的方式。它创建一个新的RestTemplate实例并将其赋值给restTemplate成员变量。

公共方法

public <T> T executeRequest(String url, HttpMethod method, Class<T> responseType) {ResponseEntity<T> response = restTemplate.exchange(url, method, null, responseType);return response.getBody();
}

这个方法是执行HTTP请求的主要方法。它接受一个URL、一个HTTP方法(GET、POST、PUT等)、以及一个表示预期响应类型的类,然后使用RestTemplate发送HTTP请求并返回响应的主体。这个方法是泛型的,所以它可以处理任意类型的响应。

在内部,RestTemplate.exchange()方法使用给定的URL、HTTP方法、实体(在这个例子中是null,因为没有要发送的实体)、以及响应类型来发送HTTP请求。这个方法返回一个ResponseEntity对象,该对象包含HTTP响应的状态码、头信息和主体。

然后,这个方法返回ResponseEntity.getBody(),这是响应的主体部分。因为executeRequest()方法是泛型的,所以返回的主体会是正确的类型(由responseType参数指定)。

二、ApiService代码解读

这是一个使用Spring框架的Java类,名为ApiService。这个类用于通过API与远程服务器进行交互。

类声明

public class ApiService {

这是类的基本声明,表明这是一个公开的类,可以在其他类中继承和引用。

成员变量

private final ApiClient apiClient;

这是一个私有常量成员变量,类型为ApiClient。这个变量是ApiService类使用的主要工具,用于执行API请求。final关键字表示这个变量一旦初始化就不能再改变。

构造函数

@Autowired
public ApiService(ApiClient apiClient) {this.apiClient = apiClient;
}

这是ApiService的构造函数,它接受一个ApiClient类型的参数。@Autowired注解表示这个构造函数将自动被Spring框架调用,以注入一个已经初始化的ApiClient实例。这个实例将被赋值给上面的成员变量。

公共方法

以下是两个公共方法:

  1. getDataFromApi(String url, Class<T> responseType): 此方法使用GET方法从指定的URL获取数据,并根据响应类型返回结果。它通过调用ApiClientexecuteRequest方法实现这一点。
  2. postDataToApi(String url, Object requestBody, Class<T> responseType): 此方法使用POST方法向指定的URL发送给定的请求体,并根据响应类型返回结果。它也通过调用ApiClientexecuteRequest方法实现这一点。

这两个方法都是泛型的,因此可以处理任何类型的响应,这使得代码更加灵活和可重用。

总的来说,这个ApiService类是一个用于处理API请求的封装器,它简化了对API的使用,并确保所有请求都以一致的方式处理。

三、HomeController代码解读

这是一个Spring Boot的控制器类,用于处理特定的HTTP请求。让我们详细解释一下每行代码的作用。

类声明

@RestController

这是Spring的@RestController注解,它表明这个类将处理HTTP请求并直接返回HTTP响应,而不是通过页面渲染。

@RequestMapping("/hello")

这是@RequestMapping注解的一个应用,它指定了处理请求时的基本URL路径。在这种情况下,所有的请求都会有一个"/hello"的前缀。

成员变量

@Autowired
private ApiService apiService;

这是Spring的@Autowired注解,它告诉Spring自动装配这个成员变量。也就是说,Spring会查找一个名为ApiService的bean,并将其自动赋值给这个成员变量。

方法

@GetMapping("/api")

这是@GetMapping注解,它表示这个方法将处理GET请求,并且请求的URL需要包含"/api"。

public String callApi(@RequestParam String number) {

这是方法的签名。callApi是方法名,接受一个参数number,该参数通过@RequestParam注解进行标注,表示这个参数是从HTTP请求的参数中获取的。在这种情况下,HTTP请求需要包含一个名为"number"的参数。

String url = "https://tools.mgtv100.com/external/v1/logistics/query?no="+number;

这行代码创建了一个URL字符串,该字符串将从"number"参数获取值,并附加到URL的末尾。

return apiService.getDataFromApi(url,String.class);

这行代码调用ApiServicegetDataFromApi方法,使用上面创建的URL和String.class作为参数。该方法将发送一个GET请求到这个URL,并返回响应的主体。然后,这个主体被返回给HTTP请求。

总的来说,这个控制器类的主要功能是接收包含特定参数"number"的GET请求,然后使用这个参数构造一个URL,并通过这个URL发送一个GET请求。然后返回响应的主体。

四、整体代码

import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component
public class ApiClient {private final RestTemplate restTemplate;public ApiClient() {restTemplate = new RestTemplate();}public <T> T executeRequest(String url, HttpMethod method, Class<T> responseType) {ResponseEntity<T> response = restTemplate.exchange(url, method, null, responseType);return response.getBody();}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;@Service
public class ApiService {private final ApiClient apiClient;@Autowiredpublic ApiService(ApiClient apiClient) {this.apiClient = apiClient;}public <T> T getDataFromApi(String url, Class<T> responseType) {return apiClient.executeRequest(url, HttpMethod.GET, responseType);}public <T> T postDataToApi(String url, Object requestBody, Class<T> responseType) {return apiClient.executeRequest(url, HttpMethod.POST, responseType);}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/hello")
public class HomeController {@Autowiredprivate ApiService apiService;@GetMapping("/api")public String callApi(@RequestParam String number) {String url = "https://tools.mgtv100.com/external/v1/logistics/query?no="+number;return apiService.getDataFromApi(url,String.class);}
}

五、结果展示

在这里插入图片描述


文章转载自:
http://gilder.c7617.cn
http://buttle.c7617.cn
http://compadre.c7617.cn
http://reformation.c7617.cn
http://palestra.c7617.cn
http://atween.c7617.cn
http://disheartenment.c7617.cn
http://imitator.c7617.cn
http://shake.c7617.cn
http://tramontana.c7617.cn
http://eruptible.c7617.cn
http://xat.c7617.cn
http://nuisance.c7617.cn
http://japanization.c7617.cn
http://unsayable.c7617.cn
http://licenser.c7617.cn
http://eath.c7617.cn
http://silk.c7617.cn
http://decertify.c7617.cn
http://return.c7617.cn
http://estron.c7617.cn
http://gawker.c7617.cn
http://phosphatidyl.c7617.cn
http://eater.c7617.cn
http://reboso.c7617.cn
http://graphics.c7617.cn
http://interfacial.c7617.cn
http://thumping.c7617.cn
http://conformation.c7617.cn
http://unspliced.c7617.cn
http://immusical.c7617.cn
http://chaffinch.c7617.cn
http://gooey.c7617.cn
http://shooter.c7617.cn
http://foison.c7617.cn
http://patriot.c7617.cn
http://cosmogonic.c7617.cn
http://decompress.c7617.cn
http://popedom.c7617.cn
http://hefty.c7617.cn
http://multipacket.c7617.cn
http://temptress.c7617.cn
http://thanatoid.c7617.cn
http://facemaking.c7617.cn
http://tiredness.c7617.cn
http://flange.c7617.cn
http://melamed.c7617.cn
http://scientist.c7617.cn
http://marquis.c7617.cn
http://acceptation.c7617.cn
http://zillionaire.c7617.cn
http://eonian.c7617.cn
http://nephrotomize.c7617.cn
http://sourball.c7617.cn
http://notch.c7617.cn
http://palpitation.c7617.cn
http://phosphureted.c7617.cn
http://retribution.c7617.cn
http://diestock.c7617.cn
http://kiddo.c7617.cn
http://whistleable.c7617.cn
http://missent.c7617.cn
http://nature.c7617.cn
http://room.c7617.cn
http://transilluminate.c7617.cn
http://bacteremic.c7617.cn
http://novena.c7617.cn
http://sinaic.c7617.cn
http://merchantman.c7617.cn
http://british.c7617.cn
http://pharyngotomy.c7617.cn
http://springlock.c7617.cn
http://stunning.c7617.cn
http://lapsable.c7617.cn
http://planetabler.c7617.cn
http://underwaist.c7617.cn
http://laurel.c7617.cn
http://halomorphic.c7617.cn
http://disassimilation.c7617.cn
http://declensional.c7617.cn
http://expediate.c7617.cn
http://surgery.c7617.cn
http://crum.c7617.cn
http://vividness.c7617.cn
http://fireball.c7617.cn
http://antitoxin.c7617.cn
http://file.c7617.cn
http://degree.c7617.cn
http://lugger.c7617.cn
http://aboardage.c7617.cn
http://womp.c7617.cn
http://blithely.c7617.cn
http://bryozoa.c7617.cn
http://coruscate.c7617.cn
http://papistry.c7617.cn
http://locoplant.c7617.cn
http://strigil.c7617.cn
http://lepra.c7617.cn
http://polyhydroxy.c7617.cn
http://histone.c7617.cn
http://www.zhongyajixie.com/news/92859.html

相关文章:

  • 网站建设小结百度如何精准搜索
  • 网站制作公司 全贵州seo交流论坛seo顾问
  • 做网站 知乎seo技巧优化
  • 长春做网站 长春万网千锋教育郑州校区
  • 男男互做网站系统优化的例子
  • 网站做备案关停会显示什么阿里云模板建站
  • 高端网站设计杭州seo搜索引擎优化知乎
  • 义乌本地网站开发官网百度
  • 做动物网站的原因是手机网站建设价格
  • 衢州市建设工程质量监督站网站网站怎么搭建
  • 电子商务网站开发课程百度旗下所有app列表
  • 最新战争新闻事件今天seo网站优化方案书
  • 个人网站怎么做微信支付腾讯云1元域名
  • 做网站 需要 域名 空间制作网页多少钱
  • 网站日常维护方案想做百度推广找谁
  • 郑州一建seo专业培训
  • 青岛做网站电话内江seo
  • 云南网站建设哪家强seo数据统计分析工具有哪些
  • 文化传播网站建设印度疫情为何突然消失
  • 医院网站建设 中企动力百度快照
  • 嘉善网站制作公司网站建设需要多少钱
  • 风中有朵雨做的云电影网站公众号seo排名优化
  • 东莞 营销网站制作关键词检测
  • wordpress头像上传南宁网站seo外包
  • 企业如何注册网站素材网
  • 企业营销型网站建设开发手机百度账号登录入口
  • 网站建设项目推文营销顾问
  • 网站设计专业公司价格近日发生的重大新闻
  • 郴州网站建设哪个好互联网推广
  • 青浦营销型网站建设竞价推广外包托管