当前位置:网站首页>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 !
边栏推荐
- MultipartFile和File互转工具类
- hooks的设计哲学
- IRS application release 16: H5 application design guide
- 深圳中国专利奖申报流程介绍,补贴100万
- Yunna provincial administrative unit fixed assets management system
- Once you know these treasure websites, you can't live without them!!!
- ava. Lang.noclassdeffounderror: org/apache/velocity/context/context solution
- The emperors of the Ming Dynasty
- Beijing Fangshan District high tech enterprise cultivation support standard, with a subsidy of 100000 yuan
- Shenzhen Nanshan District specialized special new enterprise application process, with a subsidy of RMB 100000-500000
猜你喜欢

如何下载网页照片

焱融看|混合云环境下,如何实现数据湖最优存储解决方案

threejs:如何获得几何体的boundingBox?

Yunna Qingyuan fixed assets management and barcode inventory system

记录打包GoogleChrome浏览器插件

Middleware_ Redis_ 06_ Redis transactions

Function of barcode fixed assets management system, barcode management of fixed assets

项目_基于网络爬虫的疫情数据可视化分析

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

1.4PX4程序下载
随机推荐
Function of barcode fixed assets management system, barcode management of fixed assets
SQL question brushing and sorting in leetcode of partial deduction
深圳中国专利奖政策支持介绍,补贴100万
SAS cluster analysis (system cluster, dynamic cluster fastclus, variable cluster varclus)
nodejs中使用mySql数据库
SAS factor analysis (proc factor process, factor rotation and regression method for factor score function)
负数+0+正数
[ROS introduction] cmakelist Txt and packages XML interpretation
立个flag--重构promise
SAS聚类分析(系统聚类cluster,动态聚类fastclus,变量聚类varclus)
如何使用自定义注解进行参数校验
The emperors of the Ming Dynasty
IRS application release 16: H5 application design guide
QGC ground station tutorial
Some tips for programmers to deal with stress
Conda安装Pytorch后numpy出现问题
北京企业前沿引领技术创新补助资金介绍,补助500万
Support standard for cultivation of high-tech enterprises in Changping District, Beijing, with a subsidy of 100000 yuan
对象存储 S3 在分布式文件系统中的应用
Sealem Finance打造Web3去中心化金融平台基础设施