当前位置:网站首页>Daily problem essay | 21.11.29: use resttemplate to call external put request, and prompt '400 bad request'
Daily problem essay | 21.11.29: use resttemplate to call external put request, and prompt '400 bad request'
2022-06-11 01:38:00 【Haoshuo programming】
Use resttemplate Call external put request , Tips ‘400 bad request’
Problem description : Today, call external... Inside the project put type api The following error appears :

I asked api Of url The address is the one with the parameter in the middle :/a/b/{ Parameters }/c/{ Parameters 2}
The analysis should be that the parameter is not passed in the right way , Cause the error
Solution try
After searching the Internet for some related questions , Refer to the solutions of netizens , Try the following
Try -1: Use exchange Method
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();map.add(category_id,category_value);
MultiValueMap<String, String> map2= new LinkedMultiValueMap<String, String>();
map2.add("id",parent_id);
map2.add("category_id",category_id.split("_")[0]);
try{
// encapsulation RestTemplate To tool class RestTemplateUtils
ResponseEntity responseEntity = RestTemplateUtils.put("https://...../api/v1/nodes/{id}/categories/{category_id}/",headers, map,String.class,map2);
}catch (Exception e){
log.error(e.getMessage());
}
//RestTemplate Tool class RestTemplateUtils Realization /**
* PUT Request call mode
* @param url request URL
* @param headers Request header parameters
* @param requestBody Request parameter body
* @param responseType Return object type
* @param uriVariables URL The variables in the , And Map Medium key Corresponding
* @return ResponseEntity Response object encapsulation class
*/
public static <T> ResponseEntity<T> put(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);
return geTemplate().exchange(url, HttpMethod.PUT, requestEntity, responseType, uriVariables);
}
The result of the attempt : Still wrong
Try -2: Use put Method , Modify the method in the tool class to put Method
/*
* PUT Request call mode
* @param url request URL
* @param headers Request header parameters
* @param requestBody Request parameter body
* @param responseType Return object type
* @param uriVariables URL The variables in the , And Map Medium key Corresponding
* @return ResponseEntity Response object encapsulation class
*/
public static <T> ResponseEntity<T> put(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);
geTemplate().put(url, requestEntity,uriVariables); return null; }The result of the attempt : Still failed
Try -3: After trying all kinds of methods on the Internet, it doesn't work , I use the following method modified by myself , Solve the problem successfully !!!
Solution : modify url The way of transmitting reference
// Before the change :
ResponseEntity<String> responseEntity = RestTemplateUtils.put("https://...../api/v1/nodes/{id}/categories/{category_id}/",headers, map,String.class,map2);
// After the modification :
ResponseEntity<String> responseEntity = RestTemplateUtils.put("https://...../api/v1/nodes/" + parent_id + "/categories/" + category_id.split("_")[0] + "/",headers,map,String.class,map2);
// That is, put the parameters directly into url in The result of the attempt : The request is successful !
The above is the solution to this problem , However, the problems of the program also vary according to the running environment , I hope it can be used as a reference for my friends who have the same problems !
If you don't have any suggestions now , So as not to find a solution to the same problem in the future , I mean, Ho said , See you next time !
边栏推荐
- Millions of visits - resolution of high concurrency problems
- Project_ Visual analysis of epidemic data based on Web Crawler
- 北京門頭溝區高新技術企業培育支持標准,補貼10萬
- PX4装机教程(六)垂起固定翼(倾转)
- 2.1 ros+px4 simulation - Fixed Point flight control
- 部分 力扣 LeetCode 中的SQL刷题整理
- Support standard for cultivation of high-tech enterprises in Miyun District, Beijing, with a subsidy of 100000 yuan
- 云呐|省级行政单位固定资产管理系统
- [Li mu] how to read papers [intensive reading of papers]
- The emperors of the Ming Dynasty
猜你喜欢

Multi interest recall model practice | acquisition technology

OCR文字识别经典论文详解

条码固定资产管理系统的作用,固定资产条码化管理

SAS主成分分析(求相关阵,特征值,单位特征向量,主成分表达式,贡献率和累计贡献率以及进行数据解释)

Hao expresses his opinions: what small good habits have you adhered to?

多兴趣召回模型实践|得物技术

Application of object storage S3 in distributed file system

对象存储 S3 在分布式文件系统中的应用

How to use user-defined annotation for parameter verification

Yunna PDA wireless fixed assets inventory management system
随机推荐
Bad RequestThis combination of host and port requires TLS.
2022 recognition requirements for new technologies and new products (services) in Huairou District, Beijing
Introduction to the policy support of Shenzhen China Patent Award, with a subsidy of 1million yuan
SSH Remote Login configuration sshd_ Config file details
深圳市南山区专精特新企业申报条件,补贴10-50万
如何使用自定义注解进行参数校验
Introduction to the application process of Beijing China Patent Award, with a subsidy of 1million yuan
Bad RequestThis combination of host and port requires TLS.
如何下载网页照片
PX4从放弃到精通(二十四):自定义机型
[chess life] 01 life is like chess
Summary of SAS final review knowledge points (notes on Application of multivariate statistics experiment)
Px4 installation tutorial (VI) vertical fixed wing (tilting)
1.5、PX4载具选择
云呐|PDA无线固定资产盘点管理系统
Merge sort and cardinality sort
Inventory management and strategy mode
A tutorial on building a website from scratch with complete steps (7000 words and 102 screenshots for everyone to understand, with source code attached)
Beijing Mentougou District high tech enterprise cultivation support standard, with a subsidy of 100000 yuan
From "0" to "tens of millions" concurrency, 14 technological innovations of Alibaba distributed architecture