rest API在邮递员中工作,但在春季启动中不工作

时间:2020-05-04 07:10:44

标签: spring-boot postman resttemplate

我正在尝试实现此API https://api.bnm.gov.my/portal#operation/ERLatest

根据上述URL,其GET请求带有强制接受的Herader,其值为“ application / vnd.BNM.API.v1 + json”

当我尝试邮递员时,可以得到答复->

{
    "data": [
        {
            "currency_code": "AUD",
            "unit": 1,
            "rate": {
                "date": "2020-05-04",
                "buying_rate": 2.7454,
                "selling_rate": 2.7904,
                "middle_rate": null
            }
        },
        {
            "currency_code": "CAD",
            "unit": 1,
            "rate": {
                "date": "2020-05-04",
                "buying_rate": 3.0465,
                "selling_rate": 3.0915,
                "middle_rate": null
            }
        },
        {
            "currency_code": "EUR",
            "unit": 1,
            "rate": {
                "date": "2020-05-04",
                "buying_rate": 4.7336,
                "selling_rate": 4.7786,
                "middle_rate": null
            }
        },
        {
            "currency_code": "GBP",
            "unit": 1,
            "rate": {
                "date": "2020-05-04",
                "buying_rate": 5.3769,
                "selling_rate": 5.4269,
                "middle_rate": null
            }
        },
        {
            "currency_code": "JPY",
            "unit": 100,
            "rate": {
                "date": "2020-05-04",
                "buying_rate": 4.0464,
                "selling_rate": 4.0914,
                "middle_rate": null
            }
        },
        {
            "currency_code": "SGD",
            "unit": 1,
            "rate": {
                "date": "2020-05-04",
                "buying_rate": 3.0368,
                "selling_rate": 3.0788,
                "middle_rate": null
            }
        },
        {
            "currency_code": "USD",
            "unit": 1,
            "rate": {
                "date": "2020-05-04",
                "buying_rate": 4.33,
                "selling_rate": 4.355,
                "middle_rate": null
            }
        }
    ],
    "meta": {
        "quote": "rm",
        "session": "1130",
        "last_updated": "2020-05-04 12:16:13",
        "total_result": 7
    }
}

这是我在Spring Boot应用程序中得到相同响应的操作->

@RequestMapping(value="/forex_check")
public String forexExchange() throws Exception{
    String url="https://api.bnm.gov.my/public/exchange-rate/USD";
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", "application/vnd.BNM.API.v1+json");
    HttpEntity<String> entity = new HttpEntity<String>(headers);
    ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET,entity,String.class);
    return response.getBody();
}

但是它无法获得正确的响应,即时消息->

The requested URL was rejected. Please consult with your administrator.

Your support ID is: 10497884431577109860

当我在邮递员身边玩耍时,我注意到,当我删除主机头时,我确实得到了相同类型的响应。但是据我所知,HOST标头是自动设置的。是春季启动RestTemplate不会设置此HOST标头吗?如果没有,如何手动设置?

谢谢你们。...

1 个答案:

答案 0 :(得分:1)

解决方案是手动设置User-Agent标头

在您的forexExchange()控制器方法中,

只需在设置标头的位置添加以下行: headers.set("User-Agent", "test");如下所示:

    public String forexExchange() throws Exception{
        String url="https://api.bnm.gov.my/public/exchange-rate/USD";
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.set("Accept", "application/vnd.BNM.API.v1+json");
        headers.set("User-Agent", "test");
        HttpEntity<String> entity = new HttpEntity<>(headers);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET,entity,String.class);
        return response.getBody();
    }