@Override public ServerResponse.BodyBuilder location(URI location) { this.headers.setLocation(location); return this; }
@Override public BodyBuilder location(URI location) { this.headers.setLocation(location); return this; }
/** * Set the {@code Location} header. */ public DefaultResponseCreator location(URI location) { this.headers.setLocation(location); return this; }
@Override public EntityResponse.Builder<T> location(URI location) { this.headers.setLocation(location); return this; }
@Override public BodyBuilder location(URI location) { this.headers.setLocation(location); return this; }
@RequestMapping("/to-be-redirected") public ResponseEntity<Object> redirectToExternalUrl() throws URISyntaxException { URI yahoo = new URI("http://www.yahoo.com"); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setLocation(yahoo); return new ResponseEntity<>(httpHeaders, HttpStatus.SEE_OTHER); }
@RequestMapping(value = "", method = RequestMethod.POST) @ResponseStatus(HttpStatus.CREATED) public HttpHeaders create() throws URISyntaxException { HttpHeaders headers = new HttpHeaders(); headers.setLocation(new URI("/test/items/123")); return headers; }
@RequestMapping(method = RequestMethod.POST) public ResponseEntity<?> createCustomer(UriComponentsBuilder b) { UriComponents uriComponents = b.path("/customers/{id}").buildAndExpand(id); HttpHeaders headers = new HttpHeaders(); headers.setLocation(uriComponents.toUri()); return new ResponseEntity<Void>(headers, HttpStatus.CREATED); }
/** * Send a redirect back to the HTTP client. * @param targetUrl the target URL to redirect to * @param exchange current exchange */ protected Mono<Void> sendRedirect(String targetUrl, ServerWebExchange exchange) { String transformedUrl = (isRemoteHost(targetUrl) ? targetUrl : exchange.transformUrl(targetUrl)); ServerHttpResponse response = exchange.getResponse(); response.getHeaders().setLocation(URI.create(transformedUrl)); response.setStatusCode(getStatusCode()); return Mono.empty(); }
@RequestMapping(value = "/test", method = POST, consumes = {"multipart/mixed", "multipart/form-data"}) public ResponseEntity<Object> create(@RequestPart(name = "json-data") TestData testData, @RequestPart("file-data") Optional<MultipartFile> file, @RequestPart(name = "empty-data", required = false) TestData emptyData, @RequestPart(name = "iso-8859-1-data") byte[] iso88591Data) { Assert.assertArrayEquals(new byte[]{(byte) 0xC4}, iso88591Data); String url = "http://localhost:8080/test/" + testData.getName() + "/" + file.get().getOriginalFilename(); HttpHeaders headers = new HttpHeaders(); headers.setLocation(URI.create(url)); return new ResponseEntity<>(headers, HttpStatus.CREATED); }
@Test public void location() throws URISyntaxException { URI location = new URI("http://www.example.com/hotels"); headers.setLocation(location); assertEquals("Invalid Location header", location, headers.getLocation()); assertEquals("Invalid Location header", "http://www.example.com/hotels", headers.getFirst("Location")); }
private Mono<Void> redirect(ServerHttpResponse response) { response.setStatusCode(HttpStatus.MOVED_PERMANENTLY); response.getHeaders().setLocation(URI.create("/")); return response.setComplete(); }
@Test public void postForLocationEntityContentType() throws Exception { mockSentRequest(POST, "http://example.com"); mockTextPlainHttpMessageConverter(); mockResponseStatus(HttpStatus.OK); String helloWorld = "Hello World"; HttpHeaders responseHeaders = new HttpHeaders(); URI expected = new URI("http://example.com/hotels"); responseHeaders.setLocation(expected); given(response.getHeaders()).willReturn(responseHeaders); HttpHeaders entityHeaders = new HttpHeaders(); entityHeaders.setContentType(MediaType.TEXT_PLAIN); HttpEntity<String> entity = new HttpEntity<>(helloWorld, entityHeaders); URI result = template.postForLocation("http://example.com", entity); assertEquals("Invalid POST result", expected, result); verify(response).close(); }
@Test public void postForLocationEntityCustomHeader() throws Exception { HttpHeaders requestHeaders = new HttpHeaders(); mockSentRequest(POST, "http://example.com", requestHeaders); mockTextPlainHttpMessageConverter(); mockResponseStatus(HttpStatus.OK); HttpHeaders responseHeaders = new HttpHeaders(); URI expected = new URI("http://example.com/hotels"); responseHeaders.setLocation(expected); given(response.getHeaders()).willReturn(responseHeaders); HttpHeaders entityHeaders = new HttpHeaders(); entityHeaders.set("MyHeader", "MyValue"); HttpEntity<String> entity = new HttpEntity<>("Hello World", entityHeaders); URI result = template.postForLocation("http://example.com", entity); assertEquals("Invalid POST result", expected, result); assertEquals("No custom header set", "MyValue", requestHeaders.getFirst("MyHeader")); verify(response).close(); }
@Test public void postForLocation() throws Exception { mockSentRequest(POST, "http://example.com"); mockTextPlainHttpMessageConverter(); mockResponseStatus(HttpStatus.OK); String helloWorld = "Hello World"; HttpHeaders responseHeaders = new HttpHeaders(); URI expected = new URI("http://example.com/hotels"); responseHeaders.setLocation(expected); given(response.getHeaders()).willReturn(responseHeaders); URI result = template.postForLocation("http://example.com", helloWorld); assertEquals("Invalid POST result", expected, result); verify(response).close(); }
headers.set("header", "headerValue"); headers.setContentType(MediaType.TEXT_PLAIN); headers.setLocation(new URI("/redirectFoo")); headers.put("Set-Cookie", cookieValues);
/** * Sets the location header pointing to the resource representing the given instance. Will make sure we properly * expand the URI template potentially created as self link. * * @param headers must not be {@literal null}. * @param assembler must not be {@literal null}. * @param source must not be {@literal null}. */ private void addLocationHeader(HttpHeaders headers, PersistentEntityResourceAssembler assembler, Object source) { String selfLink = assembler.getSelfLinkFor(source).getHref(); headers.setLocation(new UriTemplate(selfLink).expand()); }
target.setLocation((URI) value); target.setLocation(new URI((String) value));
@Test public void validateCustomHeaderNamePatternsAndStandardRequestHeadersMappedFromHttpHeaders() throws Exception { DefaultHttpHeaderMapper mapper = new DefaultHttpHeaderMapper(); mapper.setInboundHeaderNames(new String[] { "foo*", "HTTP_REQUEST_HEADERS" }); HttpHeaders headers = new HttpHeaders(); headers.set("foobar", "abc"); headers.setAccept(Collections.singletonList(MediaType.TEXT_XML)); headers.setLocation(new URI("http://example.org")); Map<String, ?> result = mapper.toHeaders(headers); assertEquals(2, result.size()); assertNull(result.get("Location")); assertEquals("abc", result.get("foobar")); assertEquals(MediaType.TEXT_XML, result.get("Accept")); }
@RequestMapping(method = RequestMethod.POST, value = "/{user}/customers") ResponseEntity<Customer> addCustomer(@PathVariable Long user, @RequestBody Customer c) { Customer customer = crmService.addCustomer(user, c.getFirstName(), c.getLastName()); URI uriOfNewResource = ServletUriComponentsBuilder.fromCurrentContextPath() .path("/users/{user}/customers/{customer}") .buildAndExpand(user, customer.getId()) .toUri(); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setLocation(uriOfNewResource); return new ResponseEntity<>(customer, httpHeaders, HttpStatus.CREATED); }