第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Spring Rest 模板與 https 請求的單元測試

Spring Rest 模板與 https 請求的單元測試

幕布斯7119047 2023-12-21 10:45:38
我在使用休息模板期間遇到異常,但在單元測試中沒有遇到異常 - 有什么想法嗎?    @GetMapping("/webapp/git")    public Object hit() {        return restTemplate.getForObject("https://api.twitter.com/1.1/followers/ids.json", Object.class);//          return Unirest.get("https://api.twitter.com/1.1/followers/ids.json").asString().getBody();    }org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request    at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:79) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:123) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:102) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]    at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]爪哇春天彈簧靴Spring MVC團結(jié)的
查看完整描述

1 回答

?
喵喔喔

TA貢獻1735條經(jīng)驗 獲得超5個贊

RestTemplate 不支持 200 以外的 HTTP 代碼。一種解決方案是使用 HttpStatusCodeException 捕獲其他類型的 HTTP 代碼。


try {


    restTemplate.getForObject("https://api.twitter.com/1.1/followers/ids.json", Object.class);

}

} catch (HttpStatusCodeException e) {


    if (e.getStatusCode() == HttpStatus.BAD_REQUEST) { // Http code 400

            String bodyResponse = e.getResponseBodyAsString();

            ObjectMapper mapper = new ObjectMapper();


            try {

                return mapper.readValue(bodyResponse, Object.class);

            } catch (Exception exception) {

            }


        }

如果你使用 Spring,你可以像這樣配置 Bean RestTemplate:


    @Bean

    public RestTemplate restTemplate() {        

        return new RestTemplate(new HttpComponentsClientHttpRequestFactory());

    }

我希望它有幫助。


查看完整回答
反對 回復 2023-12-21
  • 1 回答
  • 0 關注
  • 201 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號