rest API在postman中工作,但在spring boot中不工作

问题描述 投票:0回答:1

我想实现这个API https:/api.bnm.gov.myportal#operationERLatest。

按照上面的URL,其GET请求中强制接受herader,值为 "applicationvnd.BNM.API.v1+json"

当我用postman尝试时,可以得到响应-&gt。

{
    "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();
}

但它没有得到正确的响应,我得到的是-&gt。

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

Your support ID is: 10497884431577109860

当我在玩postman的时候,我注意到,当我删除HOST头时,我确实得到了同样类型的响应。但据我所知,HOST头是自动设置的。如果不是,如何手动设置?

谢谢你们....

spring-boot postman resttemplate
1个回答
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();
    }
© www.soinside.com 2019 - 2024. All rights reserved.