Spring framework request entity: what is T for a GET request?

Viewed 43

I am using the Spring framework to query an GET endpoint, creating the request as follows:

String url = ...
HttpHeaders httpHeaders = new HttpHeaders();
RequestEntity<?> requestEntity = RequestEntity.get(url).headers(httpHeaders).build();

Now obviously the compiler rejects the "?" as a type, and according to the documentation, the type specified here should be the type of the body.

But this is a GET request. There is no body. How do I use this function? (Removing the <?> altogether does not work either).

Edit: Fixed my example, which, as correctly pointed out by @Nico Van Belle, was missing the .build()

1 Answers

You should use the Void type. Normally, IDEs should be smart enough to add it via autocomplete.

RequestEntity<Void> build = RequestEntity.get(url).headers(httpHeaders).build();

Do note that your example does not return a RequestEntity, but a builder. You still have to call build() from the builder to actually build the RequestEntity object.

Related