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

3
有一个外部的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()测试方法中模拟国家的REST调用(参见上面的curl命令)?
  2. 我需要添加或更改HTTP Headers的不同值吗(在setup()方法中)?
  3. 不明白validAge是如何使用Integer包装类而String没有的?
  4. 使用Spring的RestTemplate API是否有更好的方法?
谢谢您抽出时间阅读这些问题...

我不使用RestTemplate,但我想它的默认Content-Type如果出现415错误应该不是application/json。请尝试明确将Content-Type头设置为application/json。 - Paul Samsotha
我确实在setup()方法中这样做了。 - PacificNW_Lover
不,你设置了“Accept”头。它们是两个不同的东西。 - Paul Samsotha
1个回答

1

您需要将Content-Type设置为application/json。Content-Type必须在请求中设置。以下是设置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"和头信息进行构建。
我认为这回答了你的问题1、2和4,但没有回答第3个问题。

针对你的第三个问题,我在这里使用了一个简单的示例并成功地用于字符串和整数。能否请你添加你使用字符串时出现的错误? - sag
然后请设置Content-Type头并尝试使用String。但我认为您使用的REST服务是以仅接受Integer而不是String的方式编写的。 - sag

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接