markdown 春中几种发送请求的方式
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 春中几种发送请求的方式相关的知识,希望对你有一定的参考价值。
## Feign
[OpenFeign/feign: Feign makes writing java http clients easier](https://github.com/OpenFeign/feign)
```java
public class FeignUtil {
public static <T> T getClient(Class<T> clazz) throws Exception{
return getClient(clazz,StaticContextAccessor.getBean(Environment.class).getProperty("haproxy"));
}
public static <T> T getClient(Class<T> clazz,String url) throws Exception{
return Feign.builder()
.encoder(new JacksonEncoder())
.decoder(new JacksonDecoder())
.options(new Request.Options(1000, 3500))
.retryer(new Retryer.Default(5000, 5000, 3))
.target(clazz, url);
}
}
public interface TestClient {
@RequestLine(value = "POST /getId")
@Body("id={id}")
CommonResponse<JSONObject> getId(@Param("id") Integer id);
}
CommonResponse<JSONObject> response = FeignUtil.getClient(TestClient.class).getId(asr_id);
```
## RestTemplate
[A Guide to the RestTemplate | Baeldung](https://www.baeldung.com/rest-template)
```java
@SpringBootApplication
@EnableScheduling
@EnableFeignClients
public class ScheduleTaskServerApplication {
public static void main(String[] args) {
SpringApplication.run(ScheduleTaskServerApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<>();
requestEntity.add("postData", postData);
String url = "xxx.xxx.xxx.xxx/test";
String Response = this.restTemplate.postForObject(url,requestEntity, String.class);
```
## Unirest
[Unirest - Simplified, lightweight HTTP request client libraries in multiple languages](http://unirest.io/)
```java
String url = "xxx.xxx.xxx.xxx";
Unirest.setTimeouts(500,500);//设置超时时间
MultipartBody postRequest = Unirest.post(url).field("time", new Date().getTime());
postRequest.asString();//使用asString方法时才真正发出请求
```
以上是关于markdown 春中几种发送请求的方式的主要内容,如果未能解决你的问题,请参考以下文章
golang中几种channel的关闭方式的学习与总结
iOS中几种常用的数据存储方式
Java 中几种常用的线程池
Java中几种office文档转pdf的方式
js中几种循环的退出方式
SQL Server中几种遍历方式比较