breaktop
breaktop

Reputation: 2029

GET Request Works in Postman but not with SpringBoot RestTemplate

I have a two Spring Boot application. One is a rest client that makes rest calls. Another app that has only Rest endpoint.

When the Rest client hits the rest endpoint, it fails.

This is the code used to hit the rest endpoint:

HttpHeaders headers = new HttpHeaders();
headers.set(ACCEPT, APPLICATION_JSON);
headers.set(CONTENT_TYPE, APPLICATION_JSON);
HttpEntity entity = new HttpEntity(headers);

UriComponentsBuilder builder = UriComponentsBuilder
    .fromHttpUrl(url)

    .queryParam(EMAIL, URLEncoder.encode(email, "UTF-8"))
    .queryParam(ADDRESS, URLEncoder.encode(address, "UTF-8"));

ResponseEntity<Address> response = 
commonRestTemplate
    .exchange(builder.toUriString(), 
HttpMethod.GET, entity, Address.class);

This is the rest endpoint the client is trying to hit:

@RestController
@AllArgsConstructor
public class AddressController {
    private final RestTemplate commonRestTemplate;

    // constructor and other rest endpoints


    @RequestMapping(value = "/", method = RequestMethod.GET)
    public @ResponseBody ResponseEntity<Address> getAddress(@RequestParam String email, @RequestParam String address) {
        try {
            // do soemthing
        } catch (RuntimeException e) 
        {
            LOGGER.error(e.getMessage(), e);
            return status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
   }
}

This is the error I'm seeing in the app with the rest endpoint:

2020-03-26 16:33:53.619  WARN 9 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'address' is not present]

2020-03-26 16:50:02.691 ERROR 9 --- [nio-8080-exec-9] u.c.h.s.s.controller.AddressController      : Key may not be empty

Why does the Rest call work with Postman but not my rest client?

I've also tried with and without encoding the special characters in the rest client with no luck. I can't seem to see what I am missing

Upvotes: 2

Views: 4143

Answers (3)

Ali Moshiri
Ali Moshiri

Reputation: 93

I had this problem too. It was solved when I used uri instead string in exchange method.

ResponseEntity<String> responseEntity = null;
Map<String, String> map = generate map to keep key and value of necessaryparameters;
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl("SERVICE_URL");
map.forEach((k, v) -> {
    uriComponentsBuilder.queryParam(k, v);
});
URI uri = uriComponentsBuilder.build(false).encode("windows-1256").toUri();
responseEntity = new RestTemplate().exchange(uri, HttpMethod.POST, request, String.class);

Upvotes: 1

Ben
Ben

Reputation: 54

can be 2 issues:

  1. static ADDRESS is properly defined and referring to "address".
  2. another one, address value is not null. print address value before calling restTemplate.

Upvotes: 0

Gaurav Dhiman
Gaurav Dhiman

Reputation: 1023

Try below changes

UriComponentsBuilder builder = UriComponentsBuilder .fromHttpUrl(url)

.queryParam("email", URLEncoder.encode(email, "UTF-8"))

.queryParam("address", URLEncoder.encode(address, "UTF-8"));

@RestController
@AllArgsConstructor
public class AddressController {
private final RestTemplate commonRestTemplate;

// constructor and other rest endpoints


@RequestMapping(value = "/", method = RequestMethod.GET)
public @ResponseBody ResponseEntity<Address> getAddress(@RequestParam("email") String email, @RequestParam("address") String address) {
    try {
        // do soemthing
    } catch (RuntimeException e) 
    {
        LOGGER.error(e.getMessage(), e);
        return status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
 }
}

Upvotes: 1

Related Questions