英文:
How can I pass parameter as post to endpoint using RestTemplate in springboot?
问题
I am using RestTemplate in my project to make a post request to an endpoint. Basically I have lambda function written in nestjs. I am injecting lambda service in my java project. If vehicleName if condition
passes, I would like to POST that vehicleName to the url
. Is there any suggestions on how I can achieve this?
Here is my code:
private void callLambdaService(String vehicleTypesParamValue)
{
final String url = "http://localhost:3000/dispatcher/service/api/message";
final String zMETHOD = "callLambdaService - ";
RestTemplate restTemplate = new RestTemplate();
restTemplate.exchange(url, HttpMethod.POST, vehicleName, String.class);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
String result = restTemplate.getForObject(url, String.class);
VehicleType vehicleName=null;
String[] vehicleTypes = vehicleTypesParamValue.split(",");
Set<VehicleType> results = new HashSet<>();
try
{
for (String vehicleTypeParam : vehicleTypes)
{
vehicleName =
vehicleTypeFactory.getVehicleTypeByIspName(
vehicleTypeParam);
if (vehicleName == null)
{
LOGGER.warn("No codes for products or vehicle types were supplied");
}
else if (vehicleName.equals("US Mutual Fund"))
{
LOGGER.info(zMETHOD + "Vehicles provided: "
+ vehicleName.getIspName());
}
else
{
LOGGER.warn(
String.format("Unknown vehicle type provided: [%s]",
vehicleName.getIspName()));
}
}
}catch (Exception e) {
LOG.error("Unable to get vehicletype data", e);
}
}
And the testing command:
curl -X POST "https://gdxdispatcher.dev.awstrp.net/dispatcher/service/api/message" -H "accept: */*" -H "Content-Type: application/json" -d "{\"vehicleType\":\"US Mutual Fund,VIP\",\"source\":\"PCS_DATACACHE_TOPIC\"}"
英文:
I am using RestTemplate in my project to make a post request to an endpoint. Basically I have lambda function written in nestjs. I am injecting lambda service in my java project. If vehicleName if condition
passes, I would like to POST that vehicleName to the url
. Is there any suggestions on how I can achieve this? I would be testing my application using this command
curl -X POST "https://gdxdispatcher.dev.awstrp.net/dispatcher/service/api/message" -H "accept: */*" -H "Content-Type: application/json" -d "{\"vehicleType\":\"US Mutual Fund,VIP\",\"source\":\"PCS_DATACACHE_TOPIC\"}"
Here is my code
private void callLambdaService(String vehicleTypesParamValue)
{
final String url = "http://localhost:3000/dispatcher/service/api/message";
final String zMETHOD = "callLambdaService - ";
RestTemplate restTemplate = new RestTemplate();
restTemplate.exchange("url", HttpMethod.POST, vehicleName, String.class);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
String result = restTemplate.getForObject(url, String.class);
VehicleType vehicleName=null;
String[] vehicleTypes = vehicleTypesParamValue.split(",");
Set<VehicleType> results = new HashSet<>();
try
{
for (String vehicleTypeParam : vehicleTypes)
{
vehicleName =
vehicleTypeFactory.getVehicleTypeByIspName(
vehicleTypeParam);
if (vehicleName == null)
{
LOGGER.warn("No codes for products or vehicle types were supplied");
}
else if (vehicleName.equals("US Mutual Fund"))
{
LOGGER.info(zMETHOD + "Vehicles provided: "
+ vehicleName.getIspName());
}
else
{
LOGGER.warn(
String.format("Unknown vehicle type provided: [%s]",
vehicleName.getIspName()));
}
}
}catch (Exception e) {
LOG.error("Unable to get vehicletype data", e);
}
}
</details>
# 答案1
**得分**: 1
1. 创建一个表示HTTP请求的数据结构的 **headers**。
```java
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
-
从
org.json
包中创建一个可修改的名称/值映射集合的 JSONObject,并放入名称和值。JSONObject requestBody = new JSONObject(); requestBody.put("vehicleType", "US Mutual Fund,VIP"); requestBody.put("source", "PCS_DATACACHE_TOPIC");
-
创建代表HTTP请求或响应的 HttpEntity,在这种情况下,请求由headers和body组成。
HttpEntity<String> request = new HttpEntity<>(requestBody.toString(), headers);
-
通过 posting 对象到给定的
URI
模板来创建一个新的资源。它将结果自动转换为responseType参数中指定的类型。然后,我们将ObjectNode
定义为我们的资源,响应类型定义为我们的结果。ObjectNode result = restTemplate.postForObject("https://gdxdispatcher.dev.awstrp.net/dispatcher/service/api/message", request, ObjectNode.class);
英文:
Well, following the instructions:
-
Create headers which will be a data structure representing HTTP request.
HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON);
-
Build a JSONObject from
org.json
package that is a modifiable set of name/value mappings and put names and values.JSONObject requestBody = new JSONObject(); requestBody.put("vehicleType", "US Mutual Fund,VIP"); requestBody.put("source", "PCS_DATACACHE_TOPIC");
-
Create our HttpEntity that represents an HTTP request or response, in this case request consisting of headers and body.
HttpEntity<String> request = new HttpEntity<>(requestBody.toString(), headers);
-
Create a new resource by posting an object to the given
URI
template.
It returns the result as automatically converted to the type specified in the responseType parameter.
Then we define aObjectNode
as our resource and response type as our result.ObjectNode result = restTemplate.postForObject("https://gdxdispatcher.dev.awstrp.net/dispatcher/service/api/message", request, ObjectNode.class);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论