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 restTemplate.exchange(request, User.class).getBody();
    } 


	public Customer save(Customer customer) {
		
		URI uri = UriComponentsBuilder
			.fromHttpUrl(customerHost)
			.path("/customer")
			.build(true).toUri();
		
		RequestEntity<Customer> request = 
			post(uri)
			.accept(APPLICATION_JSON).contentType(APPLICATION_JSON)
			.body(customer);
		
		ResponseEntity<Customer> result = restTemplate.exchange(request, Customer.class);
		
		return result.getBody();
	} 

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

	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);
		return builder.build();
	} 

	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)))
						.accept(MediaType.APPLICATION_JSON);
				if (user != null) {
					headersBuilder = headersBuilder.header("Authorization", getBase64(user));
				}
				request = headersBuilder.build();
			} else {
				BodyBuilder bodyBuilder = RequestEntity.post(new 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 restTemplate.exchange(request, new ParameterizedTypeReference<String>() {
			}).getBody();
		} catch (URISyntaxException e1) {
			throw new ElectionsException(20, e1);
		} catch (RuntimeException e2) {
			throw new ElectionsException(21, e2);
		}
	} 

Advertisement
Javadoc
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("http://example.com/bar")) .accept(MediaType.APPLICATION_JSON) .body(body); ResponseEntity<MyResponse> response = template.exchange(request, MyResponse.class);

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

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
Advertisement