/** * Build a {@code UriComponents} instance and replaces URI template variables * with the values from an array. This is a shortcut method which combines * calls to {@link #build()} and then {@link UriComponents#expand(Object...)}. * @param uriVariableValues the URI variable values * @return the URI components with expanded values */ public UriComponents buildAndExpand(Object... uriVariableValues) { return build().expand(uriVariableValues); }
/** * Build a {@code UriComponents} instance and replaces URI template variables * with the values from a map. This is a shortcut method which combines * calls to {@link #build()} and then {@link UriComponents#expand(Map)}. * @param uriVariables the map of URI variables * @return the URI components with expanded values */ public UriComponents buildAndExpand(Map<String, ?> uriVariables) { return build().expand(uriVariables); }
/** * Build a {@code UriComponents} instance and replaces URI template variables * with the values from a map. This is a shortcut method which combines * calls to {@link #build()} and then {@link UriComponents#expand(Map)}. * @param uriVariables the map of URI variables * @return the URI components with expanded values */ public UriComponents buildAndExpand(Map<String, ?> uriVariables) { return build().expand(uriVariables); }
/** * Build a {@code UriComponents} instance and replaces URI template variables * with the values from an array. This is a shortcut method which combines * calls to {@link #build()} and then {@link UriComponents#expand(Object...)}. * @param uriVariableValues the URI variable values * @return the URI components with expanded values */ public UriComponents buildAndExpand(Object... uriVariableValues) { return build().expand(uriVariableValues); }
@Override public URI build(Object... uriVariables) { return buildInternal(EncodingHint.ENCODE_TEMPLATE).expand(uriVariables).toUri(); }
@Override public URI build(Map<String, ?> uriVariables) { return buildInternal(EncodingHint.ENCODE_TEMPLATE).expand(uriVariables).toUri(); }
/** * Given an array of variables, expand this template into a full URI. The array represent variable values. * The order of variables is significant. * <p>Example: * <pre class="code"> * UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); * System.out.println(template.expand("Rest & Relax", 42)); * </pre> * will print: <blockquote>{@code http://example.com/hotels/Rest%20%26%20Relax/bookings/42}</blockquote> * @param uriVariableValues the array of URI variables * @return the expanded URI * @throws IllegalArgumentException if {@code uriVariables} is {@code null} * or if it does not contain sufficient variables */ public URI expand(Object... uriVariableValues) { UriComponents expandedComponents = this.uriComponents.expand(uriVariableValues); UriComponents encodedComponents = expandedComponents.encode(); return encodedComponents.toUri(); }
/** * Given the Map of variables, expands this template into a URI. The Map keys represent variable names, * the Map values variable values. The order of variables is not significant. * <p>Example: * <pre class="code"> * UriTemplate template = new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}"); * Map<String, String> uriVariables = new HashMap<String, String>(); * uriVariables.put("booking", "42"); * uriVariables.put("hotel", "Rest & Relax"); * System.out.println(template.expand(uriVariables)); * </pre> * will print: <blockquote>{@code http://example.com/hotels/Rest%20%26%20Relax/bookings/42}</blockquote> * @param uriVariables the map of URI variables * @return the expanded URI * @throws IllegalArgumentException if {@code uriVariables} is {@code null}; * or if it does not contain values for all the variable names */ public URI expand(Map<String, ?> uriVariables) { UriComponents expandedComponents = this.uriComponents.expand(uriVariables); UriComponents encodedComponents = expandedComponents.encode(); return encodedComponents.toUri(); }
@Override public URI build(Map<String, ?> uriVars) { if (!defaultUriVariables.isEmpty()) { Map<String, Object> map = new HashMap<>(); map.putAll(defaultUriVariables); map.putAll(uriVars); uriVars = map; } if (encodingMode.equals(EncodingMode.VALUES_ONLY)) { uriVars = UriUtils.encodeUriVariables(uriVars); } UriComponents uric = this.uriComponentsBuilder.build().expand(uriVars); return createUri(uric); }
@Override public URI build(Object... uriVars) { if (ObjectUtils.isEmpty(uriVars) && !defaultUriVariables.isEmpty()) { return build(Collections.emptyMap()); } if (encodingMode.equals(EncodingMode.VALUES_ONLY)) { uriVars = UriUtils.encodeUriVariables(uriVars); } UriComponents uric = this.uriComponentsBuilder.build().expand(uriVars); return createUri(uric); }
@ApiOperation("getForEntity Uri") @RequestMapping(value = "/get3/{id}", method = RequestMethod.GET) @ResponseBody public Object getForEntity3(@PathVariable Long id) { String url = HOST_MALL_ADMIN + "/brand/{id}"; UriComponents uriComponents = UriComponentsBuilder.fromUriString(url).build().expand(id).encode(); ResponseEntity<CommonResult> responseEntity = restTemplate.getForEntity(uriComponents.toUri(), CommonResult.class); return responseEntity.getBody(); }
private UriComponents buildInternal(EncodingHint hint) { UriComponents result; if (this.ssp != null) { result = new OpaqueUriComponents(this.scheme, this.ssp, this.fragment); } else { HierarchicalUriComponents uric = new HierarchicalUriComponents(this.scheme, this.fragment, this.userInfo, this.host, this.port, this.pathBuilder.build(), this.queryParams, hint == EncodingHint.FULLY_ENCODED); result = hint == EncodingHint.ENCODE_TEMPLATE ? uric.encodeTemplate(this.charset) : uric; } if (!this.uriVariables.isEmpty()) { result = result.expand(name -> this.uriVariables.getOrDefault(name, UriTemplateVariables.SKIP_VALUE)); } return result; }
@Test // SPR-13311 public void expandWithRegexVar() { String template = "/myurl/{name:[a-z]{1,5}}/show"; UriComponents uriComponents = UriComponentsBuilder.fromUriString(template).build(); uriComponents = uriComponents.expand(Collections.singletonMap("name", "test")); assertEquals("/myurl/test/show", uriComponents.getPath()); }
@Test(expected = IllegalStateException.class) public void expandEncoded() { UriComponentsBuilder.fromPath("/{foo}").build().encode().expand("bar"); }
@Test public void encodeAndExpandPartially() { UriComponents uri = UriComponentsBuilder .fromPath("/hotel list/{city} specials").queryParam("q", "{value}").encode() .uriVariables(Collections.singletonMap("city", "Z\u00fcrich")) .build(); assertEquals("/hotel%20list/Z%C3%BCrich%20specials?q=a%2Bb", uri.expand("a+b").toString()); }
@Test // SPR-17168 public void encodeAndExpandWithDollarSign() { UriComponents uri = UriComponentsBuilder.fromPath("/path").queryParam("q", "{value}").encode().build(); assertEquals("/path?q=JavaClass%241.class", uri.expand("JavaClass$1.class").toString()); }
@Test public void expandAndEncode() { UriComponents uri = UriComponentsBuilder .fromPath("/hotel list/{city} specials").queryParam("q", "{value}").build() .expand("Z\u00fcrich", "a+b").encode(); assertEquals("/hotel%20list/Z%C3%BCrich%20specials?q=a+b", uri.toString()); }
@Test public void encodeAndExpand() { UriComponents uri = UriComponentsBuilder .fromPath("/hotel list/{city} specials").queryParam("q", "{value}").encode().build() .expand("Z\u00fcrich", "a+b"); assertEquals("/hotel%20list/Z%C3%BCrich%20specials?q=a%2Bb", uri.toString()); }
@Test public void expand() { UriComponents uriComponents = UriComponentsBuilder.fromUriString( "http://example.com").path("/{foo} {bar}").build(); uriComponents = uriComponents.expand("1 2", "3 4"); assertEquals("/1 2 3 4", uriComponents.getPath()); assertEquals("http://example.com/1 2 3 4", uriComponents.toUriString()); }
@Test // SPR-12123 public void port() { UriComponents uri1 = fromUriString("http://example.com:8080/bar").build(); UriComponents uri2 = fromUriString("http://example.com/bar").port(8080).build(); UriComponents uri3 = fromUriString("http://example.com/bar").port("{port}").build().expand(8080); UriComponents uri4 = fromUriString("http://example.com/bar").port("808{digit}").build().expand(0); assertEquals(8080, uri1.getPort()); assertEquals("http://example.com:8080/bar", uri1.toUriString()); assertEquals(8080, uri2.getPort()); assertEquals("http://example.com:8080/bar", uri2.toUriString()); assertEquals(8080, uri3.getPort()); assertEquals("http://example.com:8080/bar", uri3.toUriString()); assertEquals(8080, uri4.getPort()); assertEquals("http://example.com:8080/bar", uri4.toUriString()); }