【问题标题】:rest API working in postman but not in spring bootREST API 在邮递员中工作,但在 Spring Boot 中不工作
【发布时间】:2020-08-18 13:30:30
【问题描述】:

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

根据上面的 URL,它的 GET 请求带有强制 Accept 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 标头时,我确实得到了同样类型的响应。但据我所知,主机头是自动设置的。是spring boot RestTemplate不会设置这个HOST头吗?如果不是如何手动设置?

谢谢你们……

【问题讨论】:

    标签: spring-boot postman resttemplate


    【解决方案1】:

    解决办法是手动设置User-Agent header

    在您的 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();
        }
    

    【讨论】:

    • 您能解释一下为什么需要“User-Agent”标头吗?
    • 它在他们的后端用作反垃圾邮件保护,而RestTemplate没有提供默认的,所以我们需要手动提供它
    • 有效!也谢谢你的解释。标记为正确答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 2021-03-30
    • 2017-08-16
    • 2020-05-25
    • 2016-10-23
    • 2019-08-25
    相关资源
    最近更新 更多