在java开发中,发起http请求是非常常见的需求,常用的有HttpClient,下面聊一下okhttp3的请求方式。
1、引入okhttp3依赖
<dependency>
<groupId>io.github.admin4j</groupId>
<artifactId>http</artifactId>
<version>0.4.0</version>
</dependency>
2、提供springboot工程及http接口
@RestController
public class HelloController {
/**
* @description: get请求
*/
@GetMapping("/hello")
public String hello(@RequestParam String name) {
return "hello," + name;
}
/**
* @description: get请求
*/
@GetMapping("hello2/{name}")
public String hello2(@PathVariable("name") String name) {
return "hello2," + name;
}
/**
* @description: post请求
*/
@PostMapping("hello3")
public String hello3(@RequestBody Map map) {
return map.get("name").toString();
}
/**
* @description: form表单请求
*/
@RequestMapping("hello4")

最低0.47元/天 解锁文章
103

被折叠的 条评论
为什么被折叠?



