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

I have RestTemplate like this:

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;

and hits controller which looks like this:

@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);

and it works in some cases, but for some I'm getting

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

I tried to see what will happen if I set both parameter as null, but it was working fine. What could cause this error?



Solution 1:[1]

I found why it didn't work, I was getting an empty string as internetMessageId, so I added

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

and changed controller

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

and it worked.

Solution 2:[2]

Note: My application implemented concept of service registry and context path.
I was getting the same error...
It was context path related issue for my application.
I was calling one microservice from another, but was not adding context path to microservice endpoint.
.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cojeca
Solution 2 CodeWorld