Idea calls interfaces using httpclient

Posted by willfitch on Sat, 18 May 2019 16:21:55 +0200

Preface

Blogger github

Blogger's personal blog http://blog.healerjean.com

After using swagger, I think it's very convenient, and then using postman, I think it's a little bit troublesome, but finally the data can be saved. But after using idea's httclient, I really don't want to use them anymore.

1. Opening mode

1.1. Regarding the following method, it is not recommended to use it, because using the following method is actually no different from postman.

2. Correct Ways of Use

2.1. Post Request

2.1.1,@RequestBody

If you don't write it, just follow the normal call below, but if you write in the parameters using required=true, you must not make the DTO object null, otherwise you will report an error ** org. spring framework. http. converter. HttpMessageNotReadable Exception: Required request body is missing ** or set it to 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
#Request parameters. Note that it's empty here, otherwise the call won't succeed, because if it's next to the above parameters, it's considered one of the parameters.

{"typeKey":"country","dataKey":"china","dataValue":"China"}

2.2. Get request

2.2.1. GetMapping does not support @RequestBody

But if we use the @RequestBody method for Get requests, the following post request method can also be successfully invoked. However, the normal Get request on the front end will not succeed with an address call. So get requests, just write the object directly instead of @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 request

@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 requests

@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
#Request parameters

{"id":1,"typeKey":"country","typeDesc":"international"}


Interested, welcome to add blogger Wechat

Ha, the blogger is very happy to communicate with friends from all walks of life. If you are satisfied, please reward the amount of money the blogger intends to pay. Interested in Wechat transfer, please note your Wechat or other contact information. Add the blogger Weixin.

Please leave a message below. Discuss freely with the blogger

WeChat Wechat Public Number Alipay

Topics: Programming JSON github Spring