以下是Java使用RestTemplate调用接口的示例代码:

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

public class ApiClient {

    private static final String BASE_URL = "http://100.71.8.94:32600/cuchulainn/openapi/asset";
    private static final String USERNAME = "admin";
    private static final String PASSWORD = "admin5";

    private RestTemplate restTemplate;
    private HttpHeaders headers;

    public ApiClient() {
        restTemplate = new RestTemplate();
        headers = new HttpHeaders();
        headers.setBasicAuth(USERNAME, PASSWORD);
    }

    public ResponseEntity<String> callApi() {
        HttpEntity<Object> entity = new HttpEntity<>(headers);
        return restTemplate.exchange(BASE_URL, HttpMethod.GET, entity, String.class);
    }

}

在上面的代码中,我们首先定义了接口的基础路径、用户名和密码。然后我们创建了一个RestTemplate实例,并且设置了Basic Auth的用户名和密码。我们还定义了一个callApi()方法来调用接口,并且返回一个ResponseEntity对象。在这个方法中,我们创建了一个HttpEntity对象,并将headers添加到这个对象中,最后使用exchange()方法来发送GET请求并获取响应

接口路径http1007189432600cuchulainnopenapiasset接口Basci Auth的Username是adminPassword是admin5java用restTemplate实现接口的调用

原文地址: https://www.cveoy.top/t/topic/gAZf 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录