Spring RestTemplate:BadRequest 400,为空。在调用获取请求时

时间:2019-05-08 10:08:47

标签: java spring resttemplate

我正在尝试使用RestTemplate调用get请求。我尝试如下所示,但出现错误HttpClientErrorException$BadRequest: 400 null

有人可以告诉我在调用带有参数的Get Request时我做错了什么吗?

@GetMapping("/getDepartmentInstitute")
public Integer getDeptInstByUserIDAndInstId(@RequestParam Integer nUserId,
                                            @RequestParam Integer  nDeptInst) {
return permissionsService.findDeptInstByUserIdAndInstId(nUserId,nDeptInst );        
}

我尝试过这样

public Integer findAlertDetails(Integer nUserId, Integer nInstId) {                 

    String uri="http://localhost:8064/spacestudy/" +instituteIdentifier +"/communication/alertmanagement/getDepartmentInstitute?nUserId="+ nUserId+"&nInstId="+nInstId+"";

        RestTemplate restTemplate = new RestTemplate();

       Integer nInstTo=restTemplate.getForObject(uri,Integer.class,nUserId,nInstId);

            System.out.println(nInstTo);    

        return nInstTo;
    }

2 个答案:

答案 0 :(得分:0)

restTemplate.getForObject(String url,Class responseType,Object ... uriVariables)应该在您的String url中处理诸如{myVar}之类的uri变量。

如果要设置查询参数,请考虑使用UriComponentsBuilder。

URI uri = UriComponentsBuilder.fromHttpUrl("http://localhost:8064/spacestudy/" +instituteIdentifier +"/communication/alertmanagement/getDepartmentInstitute")
              .queryParam("nUserId", nUserId)
              .queryParam("nDeptInst", nInstId)
              .build().toUri();

Integer nInstTo = restTemplate.getForObject(uri,Integer.class);

或者您可以像这样手动设置它们:

String uri = "http://localhost:8064/spacestudy/" +instituteIdentifier +"/communication/alertmanagement/getDepartmentInstitute"
                   + "?nUserId=" + nUserId + "&nDeptInst=" + nInstId;

我更喜欢UriComponentsBuilder解决方案。

答案 1 :(得分:0)

您可以作为解决方案1 ​​手动构建url,也可以将UriComponentsBuilder作为解决方案2

使用

解决方案1 ​​

public Integer findAlertDetails(Integer nUserId, Integer nInstId) {

            String uri="http://localhost:8064/spacestudy/" +instituteIdentifier +"/communication/alertmanagement/getDepartmentInstitute?nUserId="+nUserId+"&nDeptInst="+nInstId;

            RestTemplate restTemplate = new RestTemplate();
            Integer nInstTo=restTemplate.getForObject(uri,Integer.class);

            System.out.println(nInstTo);

            return nInstTo;
        }

解决方案2

public Integer findAlertDetails(Integer nUserId, Integer nInstId) {
        String uri="http://localhost:8064/spacestudy/" +instituteIdentifier +"/communication/alertmanagement/getDepartmentInstitute";

        RestTemplate  restTemplate = new RestTemplate();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Content-Type", "application/json");
        UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(uri)
                .queryParam("nUserId", nUserId)
                .queryParam("nDeptInst", nInstId);

        HttpEntity<Integer> requestEntity = new HttpEntity<>(httpHeaders);

        ResponseEntity<Integer> responseEntity = restTemplate.exchange(
                uriBuilder.toUriString(),
                HttpMethod.GET,
                requestEntity,
                Integer.class
        );


        return requestEntity.getBody();
    }
相关问题