Idea使用httpclient调用接口

1.、关于下面这种方式呢,不建议使用,因为用了下面的,其实就和postman没区别了
Idea使用httpclient调用接口
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){

}

1
2
3
4
5
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”:“中国”}

1
2
3
4
5
6
7
8
9
2.2.1、非@RequestBody 请求,正常表单
POST http://localhost.admin/api/sys/post?type=SingleLoan
Accept: /
Cache-Control: no-cache
Cookie: scf-client_SID=24c0e5778aaf4902bc6b45c42b977f4a
Content-Type: multipart/form-data; boundary=WebAppBoundary

1
2
3
4
5
6
2.2.2、文件上传
public String upload(@RequestParam( required = false) MultipartFile file, SysFileDTO fileDTO ) throws Exception {

1
2

4.1、文件上传

POST http://scf-client-web/api/sys/file/upload?type=SingleLoan
Accept: /
Cache-Control: no-cache
Cookie: scf-client_SID=24c0e5778aaf4902bc6b45c42b977f4a
Content-Type: multipart/form-data; boundary=WebAppBoundary

file 参数名,filename 文件名(我们可以自己取名字)

–WebAppBoundary
Content-Disposition: form-data; name=“file”; filename=“filename.jpg”
Content-Type: image/jpeg

< C:\Users\HealerJean\Desktop\微信图片_20190830164253.jpg
–WebAppBoundary–

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
文件路径选择,然后点击 ,进行复制路径

1569579338051

2.2、Get 请求
2.2.1、GetMapping 不支持@RequestBody
但是我们Get请求如果使用了@RequestBody这种方式,在使用下面post请求的方法也是可以调用成功的。但是前端正常的Get请求使用地址调用则不会成功。所以Get请求,直接写上对象就行了,不要写@RequestBody

@GetMapping(“getDatas”)
public String getDatas( DataDTO dataDTO) {

}
1
2
3
4
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

1
2
3
4
5
6
2.3、Delete请求
@DeleteMapping( “deleteData/{id}”)
public String deleteData(@PathVariable Long id){

}
1
2
3
4
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

1
2
3
4
5
6
2.4、PUT请求
@PutMapping(“updateData”)
public String updateData(@RequestBody(required = false)DictionaryTypeDTO typeDTO){

}
1
2
3
4
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”:“国际”}
1
2
3
4
5
6
7
8
2.5、规范
三个#号有效分隔Http请求,所以一般在注释上进行添加
############ 1、合同 #######################################

1.1、字典类型添加

POST http://localhost.admin/api/sys/dictType/add
Accept: /
Cache-Control: no-cache
Content-Type: application/json; charset=UTF-8
Cookie: scf-manager_SID=e1fd90bf-1148-4368-9fe9-018dcaf1aa0d

{“typeKey”:“sex”,“typeDesc”:“性别”}

返回结果

#"{“msg”:“新增字典数据成功”,“typeKey”:“country”,“typeDesc”:“国家”,“id”:null}"

1.2、字典类型删除

DELETE http://localhost.admin/api/sys/dictType/1
Accept: /
Cache-Control: no-cache
Content-Type: application/json; charset=UTF-8
Cookie: scf-manager_SID=e1fd90bf-1148-4368-9fe9-018dcaf1aa0d

返回结果

“{“msg”:“字典类型删除成功”}”