Idea使用httpclient调用接口
前言
博主github
博主个人博客http://blog.healerjean.com
使用过swagger、觉得这个已经很方便了,再使用postman,觉得随便麻烦一些,但总算数据可以保存了。但是使用过idea的httclient之后,真的不想再使用他们了。
1、打开方式
1.1、关于下面这种方式呢,不建议使用,因为用了下面的,其实就和postman没区别了
2、正确的使用方式
2.1、Post请求
2.1.1、@RequestBody
不写的话,按照下面的正常调用就行了,但是如果写上了里面参数使用了required=true,一定不能让DTO对象为null,否则就会报错**org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing**
或者将它设置为required=false
@PostMapping("addData")
public String addData(@RequestBody(required = false) DataDTO dataDTO){
}
POST http://localhost.admin/addData
Accept: */*
Cache-Control: no-cache
Content-Type: application/json; charset=UTF-8
Cookie: JSESSIONID=e1fd90bf-1148-4368-9fe9-018dcaf1aa0d
#请求参数,注意这里要空一样,否则就不会调用成功,因为如果和上面的参数紧挨着,就会被认为是参数的一种
{"typeKey":"country","dataKey":"china","dataValue":"中国"}
2.2、Get 请求
2.2.1、GetMapping 不支持@RequestBody
但是我们Get请求如果使用了@RequestBody这种方式,在使用下面post请求的方法也是可以调用成功的。但是前端正常的Get请求使用地址调用则不会成功。所以Get请求,直接写上对象就行了,不要写@RequestBody
@GetMapping("getDatas")
public String getDatas( DataDTO dataDTO) {
}
GET http://localhost.admin/getDatas?typeKey=country&dataKey=1
Accept: */*
Cache-Control: no-cache
Content-Type: application/json; charset=UTF-8
Cookie: JSESSIONID=a6ad800b-15cf-4148-b659-a604e5a5f0fc
2.3、Delete请求
@DeleteMapping( "deleteData/{id}")
public String deleteData(@PathVariable Long id){
}
DELETE http://localhost.admin/delete/1
Accept: */*
Cache-Control: no-cache
Content-Type: application/json; charset=UTF-8
Cookie: JSESSIONID=e1fd90bf-1148-4368-9fe9-018dcaf1aa0d
2.4、PUT请求
@PutMapping("updateData")
public String updateData(@RequestBody(required = false)DictionaryTypeDTO typeDTO){
}
PUT http://localhost.admin/api/sys/updateDictType
Accept: */*
Cache-Control: no-cache
Content-Type: application/json; charset=UTF-8
Cookie: JSESSIONID=e1fd90bf-1148-4368-9fe9-018dcaf1aa0d
#请求参数
{"id":1,"typeKey":"country","typeDesc":"国际"}
感兴趣的,欢迎添加博主微信
哈,博主很乐意和各路好友交流,如果满意,请打赏博主任意金额,感兴趣的在微信转账的时候,备注您的微信或者其他联系方式。添加博主微信哦。
请下方留言吧。可与博主*讨论哦
微信 | 微信公众号 | 支付宝 |
---|---|---|