org.springframework.web.client.HttpClientErrorException:404 null

时间:2018-04-25 14:08:03

标签: spring-boot controller http-status-code-404 resttemplate

我有这样的RestTemplate:

RestTemplate restTemplate = new RestTemplate();
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<?> requestEntity = new HttpEntity<>(requestHeaders);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);

        internetMessageId = null;
        projectId = null;
        EmailJSON emailJSON = null;
        String path = urlProps.getRegionalUsUrl() + Constants.Url.Email.EMAIL_FIND_EMAIL_BY_INTERNET_MESSAGE_ID_AND_PROJECT_ID
+ "/" + internetMessageId + "/";
        if(projectId != null) {
            path += "?projectId=" + projectId;
        }
        ResponseEntity<EmailJSON> responseEntity = restTemplate.exchange(path, HttpMethod.POST, requestEntity, EmailJSON.class);
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            emailJSON = responseEntity.getBody();
        }

        return  emailJSON;

并点击控制器,如下所示:

@RequestMapping(value = "/getEmailByInternetMessageIdAndProjectId/{internetMessageId}", method = RequestMethod.GET, produces = "application/json")
    public @ResponseBody EmailJSON getEmailByInternetMessageId(@PathVariable("internetMessageId") String internetMessageId, @RequestParam(value = "projectId", required = false) String projectId) {

        return emailService.getEmailByInternetMessageIdAndProjectId(internetMessageId, projectId);

它在某些情况下有效,但对某些人来说

org.springframework.web.client.HttpClientErrorException: 404 null
    2         at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91) ~[spring-web-4.3.6      .RELEASE.jar!/:4.3.6.RELEASE]
    1         at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700) ~[spring-web-4.3.6.RELEASE.jar!/:4.3.6.RELEA      SE]
37664         at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653) ~[spring-web-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
    1         at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613) ~[spring-web-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
    2         at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:531) ~[spring-web-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
    3         at com.copperdm.global.web.service.utils.SharedServicesUtils.findEmailByInternetMessageIdAndProjectId(SharedServicesUtils.java:98

我试着看看如果我将两个参数都设置为null会发生什么,但它工作正常。什么可能导致此错误?

1 个答案:

答案 0 :(得分:0)

我发现为什么它不起作用,我得到一个空字符串作为internetMessageId,所以我添加了

MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
        requestBody.set("internetMessageId", internetMessageId);
        requestBody.set("projectId", projectId);

并更改了控制器

@RequestMapping(value = "/getEmailByInternetMessageIdAndProjectId", method = RequestMethod.POST, produces = "application/json")
    public @ResponseBody EmailJSON getEmailByInternetMessageId(@RequestBody String internetMessageId,@RequestBody String projectId)

并且有效。