具有路径变量的RestTemplate GET方法

时间:2018-08-20 03:02:33

标签: java spring spring-boot httpclient resttemplate

我正在尝试使用rest模板进行rest呼叫。我为API调用设置了两个标头参数和一个路径变量。下面是我的实现。但是我收到HttpServerErrorException:500空。我是否以正确的方式设置了路径变量?

目标API:drs / v1 / {caseId}

String url = configProperties.getCaseCreateUrl();
                if(!StringUtils.isEmpty(url)){
                        url = url.replace(ApplicationConstants.DOMAIN_NAME,currentUser.domainUrl());
                        url = url+ caseId;
                }
                HttpHeaders headers = new HttpHeaders();
                headers.set(ApplicationConstants.PS_TOKEN_HEADER, currentUser.getToken());
                headers.set(ApplicationConstants.WORGROUP_HEADER, currentUser.getWorkgroupId());
                headers.set(ApplicationConstants.DOMAIN_HEADER, currentUser.getDomainId());
                headers.set(HttpHeaders.CONTENT_TYPE, MediaType.ALL_VALUE);
                HttpsTrustManager.allowAllSSL();
                RestTemplate restTemplate = new RestTemplate();
                HttpEntity<Map<String, String>> requestEntity = new HttpEntity(null, headers);
                ResponseEntity<CaseDetailsDTO> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, CaseDetailsDTO.class);

1 个答案:

答案 0 :(得分:0)

我提供了带有路径变量示例的 RestTemplate GET 方法的代码片段

public ResponseEntity<List<String>> getNames(long id) {
        
            final String url = "https://some/{id}/name";
            //with cookies
            ResponseEntity<String> cookie = getCookies();
            
            String set_cookie = cookie.getHeaders().getFirst(HttpHeaders.SET_COOKIE);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            headers.add("Cookie", set_cookie);
            HttpEntity request = new HttpEntity(headers);
            
            ResponseEntity<List<String>> response = restTemplate.exchange(url, HttpMethod.GET, request,new ParameterizedTypeReference<List<String>>(){},id);
            return response;
    }