Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading