Java Code Examples for org.springframework.http.RequestEntity

Following code examples demonstrate how to use org.springframework.http.RequestEntityfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.http.RequestEntityand various code implementation of this class.

    public User getAuthenticatedUser() {
        RequestEntity<Void> request = get(url).header(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON_VALUE).build();
        return, User.class).getBody();

	public Customer save(Customer customer) {
		URI uri = UriComponentsBuilder
		RequestEntity<Customer> request = 
		ResponseEntity<Customer> result =, Customer.class);
		return result.getBody();

	private RequestEntity<Void> prepareOptions(String url, HttpHeaders headers) throws Exception {
		RequestEntity.HeadersBuilder<?> builder = options(uri);
		addHeaders(builder, headers);

	private RequestEntity<?> preparePost(String url, HttpHeaders headers, Object body) throws Exception {
		RequestEntity.BodyBuilder builder = post(uri);
		addHeaders(builder, headers);
		return builder.body(body);

	private RequestEntity<Void> prepareGet(String url, HttpHeaders headers) throws Exception {
		RequestEntity.HeadersBuilder<?> builder = get(uri);
		addHeaders(builder, headers);

	public String getResponse(User user, String url, String jsonPost) {
		try {
			RequestEntity<?> request;
			if (jsonPost == null) {
				HeadersBuilder<?> headersBuilder = RequestEntity.get(new URI(String.format("%s%s", urlServiceWebJson, url)))
				if (user != null) {
					headersBuilder = headersBuilder.header("Authorization", getBase64(user));
				request =;
			} else {
				BodyBuilder bodyBuilder = URI(String.format("%s%s", urlServiceWebJson, url)))
						.header("Content-Type", "application/json").accept(MediaType.APPLICATION_JSON);
				if (user != null) {
					bodyBuilder = bodyBuilder.header("Authorization", getBase64(user));
				request = bodyBuilder.body(jsonPost);

			return, new ParameterizedTypeReference<String>() {
		} catch (URISyntaxException e1) {
			throw new ElectionsException(20, e1);
		} catch (RuntimeException e2) {
			throw new ElectionsException(21, e2);

Extension of HttpEntity that adds a {@linkplain HttpMethod method and {@linkplain URI uri. Used in R

estTemplate and @Controller methods.

In RestTemplate, this class is used as parameter in org.springframework.web.client.RestTemplate#exchange(RequestEntity, Class) exchange(): MyRequest body = ... RequestEntity<MyRequest> request = RequestEntity .post(new URI("")) .accept(MediaType.APPLICATION_JSON) .body(body); ResponseEntity<MyResponse> response =, MyResponse.class);

If you would like to provide a URI template with variables, consider using org.springframework.web.util.UriTemplate: URI uri = new UriTemplate("{foo").expand("bar"); RequestEntity<MyRequest> request =;

Can also be used in Spring MVC, as a parameter in a @Controller method: @RequestMapping("/handle") public void handle(RequestEntity<String> request) { HttpMethod method = request.getMethod(); URI url = request.getUrl(); String body = request.getBody(); @author Arjen Poutsma @author Sebastien Deleuze @since 4.1 @param the body type @see #getMethod() @see #getUrl()

Read More