/** * Return an {@link HttpEntity} for the given part Object. * @param part the part to return an {@link HttpEntity} for * @return the part Object itself it is an {@link HttpEntity}, * or a newly built {@link HttpEntity} wrapper for that part */ protected HttpEntity<?> getHttpEntity(Object part) { return (part instanceof HttpEntity ? (HttpEntity<?>) part : new HttpEntity<>(part)); }
@Test public void noHeaders() { String body = "foo"; HttpEntity<String> entity = new HttpEntity<>(body); assertSame(body, entity.getBody()); assertTrue(entity.getHeaders().isEmpty()); }
@Test public void testEquals() { MultiValueMap<String, String> map1 = new LinkedMultiValueMap<>(); map1.set("Content-Type", "text/plain"); MultiValueMap<String, String> map2 = new LinkedMultiValueMap<>(); map2.set("Content-Type", "application/json"); assertTrue(new HttpEntity<>().equals(new HttpEntity<Object>())); assertFalse(new HttpEntity<>(map1).equals(new HttpEntity<Object>())); assertFalse(new HttpEntity<>().equals(new HttpEntity<Object>(map2))); assertTrue(new HttpEntity<>(map1).equals(new HttpEntity<Object>(map1))); assertFalse(new HttpEntity<>(map1).equals(new HttpEntity<Object>(map2))); assertTrue(new HttpEntity<String>(null, null).equals(new HttpEntity<String>(null, null))); assertFalse(new HttpEntity<>("foo", null).equals(new HttpEntity<String>(null, null))); assertFalse(new HttpEntity<String>(null, null).equals(new HttpEntity<>("bar", null))); assertTrue(new HttpEntity<>("foo", map1).equals(new HttpEntity<String>("foo", map1))); assertFalse(new HttpEntity<>("foo", map1).equals(new HttpEntity<String>("bar", map1))); }
partBody = httpEntity.getBody(); partHeaders.addAll(httpEntity.getHeaders());
@Test public void exchangePost() throws Exception { HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.set("MyHeader", "MyValue"); requestHeaders.setContentType(MediaType.TEXT_PLAIN); HttpEntity<String> entity = new HttpEntity<>(helloWorld, requestHeaders); HttpEntity<Void> result = template.exchange(baseUrl + "/{method}", POST, entity, Void.class, "post"); assertEquals("Invalid location", new URI(baseUrl + "/post/1"), result.getHeaders().getLocation()); assertFalse(result.hasBody()); }
@RequestMapping @ResponseBody public JacksonViewBean handleHttpEntity(@JsonView(MyJacksonView1.class) HttpEntity<JacksonViewBean> entity) { return entity.getBody(); }
@SuppressWarnings("unchecked") private <T> HttpEntity<T> resolveValueWithEmptyBody(ResolvableType type) { ServerWebExchange exchange = MockServerWebExchange.from(post("/path")); MethodParameter param = this.testMethod.arg(type); Mono<Object> result = this.resolver.resolveArgument(param, new BindingContext(), exchange); HttpEntity<String> httpEntity = (HttpEntity<String>) result.block(Duration.ofSeconds(5)); assertEquals(exchange.getRequest().getHeaders(), httpEntity.getHeaders()); return (HttpEntity<T>) httpEntity; }
@Test public void responseEntity() { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_PLAIN); String body = "foo"; HttpEntity<String> httpEntity = new HttpEntity<>(body, headers); ResponseEntity<String> responseEntity = new ResponseEntity<>(body, headers, HttpStatus.OK); ResponseEntity<String> responseEntity2 = new ResponseEntity<>(body, headers, HttpStatus.OK); assertEquals(body, responseEntity.getBody()); assertEquals(MediaType.TEXT_PLAIN, responseEntity.getHeaders().getContentType()); assertEquals("text/plain", responseEntity.getHeaders().getFirst("Content-Type")); assertEquals("text/plain", responseEntity.getHeaders().getFirst("Content-Type")); assertFalse(httpEntity.equals(responseEntity)); assertFalse(responseEntity.equals(httpEntity)); assertTrue(responseEntity.equals(responseEntity2)); assertTrue(responseEntity2.equals(responseEntity)); }
@PostMapping("/foo") public ResponseEntity<String> foo(HttpEntity<byte[]> requestEntity) throws Exception { assertNotNull(requestEntity); assertEquals("MyValue", requestEntity.getHeaders().getFirst("MyRequestHeader")); String body = new String(requestEntity.getBody(), "UTF-8"); assertEquals("Hello World", body); URI location = new URI("/foo"); return ResponseEntity.created(location).header("MyResponseHeader", "MyValue").body(body); }
@PostMapping("/request/entity/mono") public Mono<JacksonViewBean> entityMonoRequest(@JsonView(MyJacksonView1.class) HttpEntity<Mono<JacksonViewBean>> entityMono) { return entityMono.getBody(); }
public HttpEntity<?> build() { return new HttpEntity<>(this.body, this.headers); } }
@Test public void httpHeaders() { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_PLAIN); String body = "foo"; HttpEntity<String> entity = new HttpEntity<>(body, headers); assertEquals(body, entity.getBody()); assertEquals(MediaType.TEXT_PLAIN, entity.getHeaders().getContentType()); assertEquals("text/plain", entity.getHeaders().getFirst("Content-Type")); }
@Test public void requestEntity() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_PLAIN); String body = "foo"; HttpEntity<String> httpEntity = new HttpEntity<>(body, headers); RequestEntity<String> requestEntity = new RequestEntity<>(body, headers, HttpMethod.GET, new URI("/")); RequestEntity<String> requestEntity2 = new RequestEntity<>(body, headers, HttpMethod.GET, new URI("/")); assertEquals(body, requestEntity.getBody()); assertEquals(MediaType.TEXT_PLAIN, requestEntity.getHeaders().getContentType()); assertEquals("text/plain", requestEntity.getHeaders().getFirst("Content-Type")); assertEquals("text/plain", requestEntity.getHeaders().getFirst("Content-Type")); assertFalse(httpEntity.equals(requestEntity)); assertFalse(requestEntity.equals(httpEntity)); assertTrue(requestEntity.equals(requestEntity2)); assertTrue(requestEntity2.equals(requestEntity)); }
@SuppressWarnings("unchecked") private void writePart(String name, HttpEntity<?> partEntity, OutputStream os) throws IOException { Object partBody = partEntity.getBody(); if (partBody == null) { throw new IllegalStateException("Empty body for part '" + name + "': " + partEntity); } Class<?> partType = partBody.getClass(); HttpHeaders partHeaders = partEntity.getHeaders(); MediaType partContentType = partHeaders.getContentType(); for (HttpMessageConverter<?> messageConverter : this.partConverters) { if (messageConverter.canWrite(partType, partContentType)) { Charset charset = isFilenameCharsetSet() ? StandardCharsets.US_ASCII : this.charset; HttpOutputMessage multipartMessage = new MultipartHttpOutputMessage(os, charset); multipartMessage.getHeaders().setContentDispositionFormData(name, getFilename(partBody)); if (!partHeaders.isEmpty()) { multipartMessage.getHeaders().putAll(partHeaders); } ((HttpMessageConverter<Object>) messageConverter).write(partBody, partContentType, multipartMessage); return; } } throw new HttpMessageNotWritableException("Could not write request: no suitable HttpMessageConverter " + "found for request type [" + partType.getName() + "]"); }
@PostMapping("/request/entity/flux") public Flux<JacksonViewBean> entityFluxRequest(@JsonView(MyJacksonView1.class) HttpEntity<Flux<JacksonViewBean>> entityFlux) { return entityFlux.getBody(); }
/** * Return an {@link HttpEntity} for the given part Object. * @param part the part to return an {@link HttpEntity} for * @return the part Object itself it is an {@link HttpEntity}, * or a newly built {@link HttpEntity} wrapper for that part */ protected HttpEntity<?> getHttpEntity(Object part) { return (part instanceof HttpEntity ? (HttpEntity<?>) part : new HttpEntity<>(part)); }
@Test public void multiValueMap() { MultiValueMap<String, String> map = new LinkedMultiValueMap<>(); map.set("Content-Type", "text/plain"); String body = "foo"; HttpEntity<String> entity = new HttpEntity<>(body, map); assertEquals(body, entity.getBody()); assertEquals(MediaType.TEXT_PLAIN, entity.getHeaders().getContentType()); assertEquals("text/plain", entity.getHeaders().getFirst("Content-Type")); }
if (value instanceof HttpEntity) { HttpEntity<T> httpEntity = (HttpEntity<T>) value; outputHeaders.putAll(httpEntity.getHeaders()); body = httpEntity.getBody(); Assert.state(body != null, "MultipartHttpMessageWriter only supports HttpEntity with body");
public ResponseEntity<String> handleHttpEntity(HttpEntity<byte[]> httpEntity) throws Exception { String responseBody = "Handled requestBody=[" + new String(httpEntity.getBody(), "UTF-8") + "]"; return ResponseEntity.accepted() .header("header", "headerValue") .body(responseBody); }
public HttpEntityRequestCallback(@Nullable Object requestBody, @Nullable Type responseType) { super(responseType); if (requestBody instanceof HttpEntity) { this.requestEntity = (HttpEntity<?>) requestBody; } else if (requestBody != null) { this.requestEntity = new HttpEntity<>(requestBody); } else { this.requestEntity = HttpEntity.EMPTY; } }