gpt4 book ai didi

spring - 如何使用 Spring 的 RestTemplate 模拟 curl REST 调用?

转载 作者:行者123 更新时间:2023-12-05 06:42:58 25 4
gpt4 key购买 nike

有一个外部 Restful Web 服务,如果它是多个输入,它接收 JSON 有效负载,但如果它是单个输入,它只需要值。

例如,对于多个输入,这有效:

curl -H "Content-Type: application/json" -X POST -d '{ "amount": 10000, "interestRate": ".28", "term": "12", "state": "Georgia"}' http://localhost:8080/webservices/REST/sample/loan

返回:

Approved

对于单一输入:

curl -H "Content-Type: application/json" -X POST -d "18" http://localhost:8080/webservices/REST/sample/age

返回:

Approved

使用 Spring Boot,尝试创建一个 JUnit 测试,以查看我是否可以使用 Spring 的 RestTemplate API 发布到此外部服务。

public void RestWebServiceTest {

private RestTemplate restTemplate;
private HttpHeaders headers;

@Before
public void setup() {
restTemplate = new RestTemplate();
headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
}

@Test
public void validLoan() {
final String uri = "http://localhost:8080/webservices/REST/sample/Loan";
Map<String, String> input = new HashMap<>();
input.put("amount", "10000");
input.put("interestRate", ".28");
input.put("term", "12");
input.put("state", "Georgia");
String result = restTemplate.postForObject(uri, input, String.class);
assertEquals("Approved", result);
}

@Test
public void validAge() {
final String uri = "http://localhost:8080/webservices/REST/sample/age";
Integer input = 18;
String result = restTemplate.postForObject(uri, input, String.class);
assertEquals("Approved", result);
}

@Test
public void validCountry() {
final String uri = "http://localhost:8080/webservices/REST/sample/country
String input = "US";
String result = restTemplate.postForObject(uri, input, String.class);
assertEquals("Approved", result);
}
}

除 validCountry() 测试方法外,所有这些都有效:

org.springframework.web.client.HttpClientErrorException: 415 Unsupported Media Type
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:641)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:597)

这很奇怪,因为这个 curl 命令适用于同一个调用:

curl -H "Content-Type: application/json" -X POST -d 'US' http://localhost:8080/webservices/REST/sample/country

返回:

Approved

问题:

  1. 如何在 validCountry() 测试方法中模拟对国家/地区的其余调用(参见上面的 curl 命令)?

  2. 我是否需要为 HTTP header 添加或更改不同的值(在 setup() 方法中)?

  3. 不明白 validAge 是通过使用 Integer 包装器类来工作的,而 String 却不能?

  4. 使用 Spring 的 RestTemplate API 是否有更好的方法来做到这一点?

感谢您花时间阅读本文...

最佳答案

您需要将 Content-Type 设置为 application/json。 Content-Type 必须在请求中设置。下面是设置内容类型的修改代码

@Test
public void validCountry() {
final String uri = "http://localhost:8080/webservices/REST/sample/country";
String input = "US";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> request = new HttpEntity<String>(input, headers);
String result = restTemplate.postForObject(uri, request, String.class);
assertEquals("Approved", result);
}

在这里,HttpEntity 是用您的输入(即“US”)和 header 构建的。

我认为这回答了您的问题 1、2 和 4。但不是 3。

关于spring - 如何使用 Spring 的 RestTemplate 模拟 curl REST 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35191699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com