美文网首页
RestTemplate调用远程服务

RestTemplate调用远程服务

作者: 祝家庄打烊 | 来源:发表于2024-11-01 17:42 被阅读0次
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";
        // 设置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"));
}

相关文章

网友评论

      本文标题:RestTemplate调用远程服务

      本文链接:https://www.haomeiwen.com/subject/dnhwdjtx.html