当前位置:网站首页>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 !
边栏推荐
- 懒汉式单例模式
- Inventory management and strategy mode
- 【ROS】ROSmsg cakin_ Make compilation error
- 关于mobx
- Implementing MySQL fuzzy search with node and express
- [geometric vision] 4.2 piecewise linear transformation
- 中国专利奖奖金多少,补贴100万
- 腾讯云数据库TDSQL-大咖论道 | 基础软件的过去、现在、未来
- 2.0、ROS与PX4通信详解
- SQL question brushing and sorting in leetcode of partial deduction
猜你喜欢

云呐|庆远固定资产管理及条码盘点系统

Simple select sort and heap sort

What are programmers in big factories looking at? It took me two years to sort it out, and I will look at it and cherish it!

Middleware_ Redis_ 06_ Redis transactions

QGC地面站使用教程
![[path planning] week 1: hodgepodge](/img/80/074b847c6826b306318aeb9866a829.jpg)
[path planning] week 1: hodgepodge

Once you know these treasure websites, you can't live without them!!!

PX4从放弃到精通(二十四):自定义机型

node和express实现mySql模糊搜索

2.2、ROS+PX4仿真多点巡航飞行----正方形
随机推荐
项目_基于网络爬虫的疫情数据可视化分析
SAS discriminant analysis (Bayes criterion and proc discrim process)
Yunna provincial administrative unit fixed assets management system
Understanding of multithreading
Web3 ecological decentralized financial platform sealem Finance
threejs:如何获得几何体的boundingBox?
简述自定义注解
北京平谷区高新技术企业培育支持标准,补贴10万
使用 CompletableFuture
Once you know these treasure websites, you can't live without them!!!
Introduction to the application process of Beijing China Patent Award, with a subsidy of 1million yuan
threejs:两点坐标绘制贝赛尔曲线遇到的坑
Middleware_ Redis_ 06_ Redis transactions
2022年高考加油 那年我的高考如此兴奋
Sealem Finance打造Web3去中心化金融平台基础设施
IRS应用发布之十五:应用安全自测指南
函数的节流和防抖
SAS期末复习知识点总结(应用多元统计实验笔记)
[mavros] mavros startup Guide
负数+0+正数