RestTemplate調(diào)用遠程服務(wù)

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

}
@Autowired
private RestTemplate restTemplate;
public String callRemotePostService(String outTransId, String tm) {
        System.out.println("支付訂單:" + outTransId + "查找月份:" + tm);
        String url = "https://yy.yb";
        // 設(shè)置Cookie
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set(HttpHeaders.COOKIE, "JSESSIONID=3c");

        String requestBody = "{\"startDate\":\"2024"+tm+"01\",\"endDate\":\"2024"+tm+"31\",\"outTransId\":"+outTransId+",\"pageNum\":1,\"pageSize\":10}";
        HttpEntity<Object> entity = new HttpEntity<>(requestBody, headers);

        ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class);

        return response.getBody();
}
String result = callRemotePostService("4306021991111", "11");
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Map<String, ArrayList<Map>>> user = objectMapper.readValue(result, Map.class);
if (user.get("data").get("list").size() > 0) {
      Map<String, String> item = user.get("data").get("list").get(0);
      System.out.println("車牌號:" + item.get("deviceNo"));
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容