UriComponents uriComponents =UriComponentsBuilder.fromUriString("https://example.com/hotels/{hotel}") .queryParam("q","{q}") .encode() .build(); URI uri =uriComponents.expand("Westin","123").toUri();
可以将前面的示例合并为一个链,并通过buildAndExpand进行缩短,如以下示例所示:
URI uri =UriComponentsBuilder.fromUriString("https://example.com/hotels/{hotel}").queryParam("q","{q}").encode().buildAndExpand("Westin","123").toUri();
您可以通过直接转到URI(这意味着编码)来进一步缩短它,如以下示例所示:
URI uri =UriComponentsBuilder.fromUriString("https://example.com/hotels/{hotel}").queryParam("q","{q}").build("Westin","123");
您可以使用完整的URI模板进一步缩短它,如以下示例所示:
URI uri =UriComponentsBuilder.fromUriString("https://example.com/hotels/{hotel}?q={q}").build("Westin","123");
URI uri =UriComponentsBuilder.fromPath("/hotel list/{city}").queryParam("q","{q}").encode().buildAndExpand("New York","foo+bar").toUri();// Result is "/hotel%20list/New%20York?q=foo%2Bbar"
您可以通过直接转到URI(这意味着编码)来缩短前面的示例,如以下示例所示:
URI uri =UriComponentsBuilder.fromPath("/hotel list/{city}").queryParam("q","{q}").build("New York","foo+bar")
您可以使用完整的URI模板进一步缩短它,如以下示例所示:
URI uri =UriComponentsBuilder.fromPath("/hotel list/{city}?q={q}").build("New York","foo+bar")
// Re-uses host, port and context path...ServletUriComponentsBuilder ucb =ServletUriComponentsBuilder.fromContextPath(request).path("/accounts").build()
UriComponents uriComponents =MvcUriComponentsBuilder.fromMethodName(BookingController.class,"getBooking",21).buildAndExpand(42);URI uri =uriComponents.encode().toUri();
UriComponents uriComponents =MvcUriComponentsBuilder.fromMethodCall(on(BookingController.class).getBooking(21)).buildAndExpand(42);URI uri =uriComponents.encode().toUri();
UriComponentsBuilder base =ServletUriComponentsBuilder.fromCurrentContextPath().path("/en");MvcUriComponentsBuilder builder =MvcUriComponentsBuilder.relativeTo(base);builder.withMethodCall(on(BookingController.class).getBooking(21)).buildAndExpand(42);URI uri =uriComponents.encode().toUri();