/** * An alternative to {@link #fromController(Class)} for use with an instance * of this class created via a call to {@link #relativeTo}. * <p><strong>Note:</strong> This method extracts values from "Forwarded" * and "X-Forwarded-*" headers if found. See class-level docs. * @since 4.2 */ public UriComponentsBuilder withController(Class<?> controllerType) { return fromController(this.baseUrl, controllerType); }
/** * Create a {@link UriComponentsBuilder} from the mapping of a controller class * and current request information including Servlet mapping. If the controller * contains multiple mappings, only the first one is used. * <p><strong>Note:</strong> This method extracts values from "Forwarded" * and "X-Forwarded-*" headers if found. See class-level docs. * @param controllerType the controller to build a URI for * @return a UriComponentsBuilder instance (never {@code null}) */ public static UriComponentsBuilder fromController(Class<?> controllerType) { return fromController(null, controllerType); }
/** * An alternative to {@link #fromController(Class)} for use with an instance * of this class created via a call to {@link #relativeTo}. * <p><strong>Note:</strong> This method extracts values from "Forwarded" * and "X-Forwarded-*" headers if found. See class-level docs. * @since 4.2 */ public UriComponentsBuilder withController(Class<?> controllerType) { return fromController(this.baseUrl, controllerType); }
/** * Create a {@link UriComponentsBuilder} from the mapping of a controller class * and current request information including Servlet mapping. If the controller * contains multiple mappings, only the first one is used. * <p><strong>Note:</strong> This method extracts values from "Forwarded" * and "X-Forwarded-*" headers if found. See class-level docs. * @param controllerType the controller to build a URI for * @return a UriComponentsBuilder instance (never {@code null}) */ public static UriComponentsBuilder fromController(Class<?> controllerType) { return fromController(null, controllerType); }
@Test public void fromControllerNotMapped() { UriComponents uriComponents = fromController(UnmappedController.class).build(); assertThat(uriComponents.toUriString(), is("http://localhost/")); }
@Test public void fromControllerTwoTypeLevelMappings() { UriComponents uriComponents = fromController(InvalidController.class).build(); assertThat(uriComponents.toUriString(), is("http://localhost/persons")); }
@Test public void fromControllerPlain() { UriComponents uriComponents = fromController(PersonControllerImpl.class).build(); assertThat(uriComponents.toUriString(), Matchers.endsWith("/people")); }
@Test public void fromControllerSubResource() { UriComponents uriComponents = fromController(PersonControllerImpl.class).pathSegment("something").build(); assertThat(uriComponents.toUriString(), endsWith("/people/something")); }
@Test public void usesForwardedHostAndPortFromHeader() throws Exception { request.addHeader("X-Forwarded-Host", "foobar:8088"); adaptRequestFromForwardedHeaders(); UriComponents uriComponents = fromController(PersonControllerImpl.class).build(); assertThat(uriComponents.toUriString(), startsWith("http://foobar:8088")); }
@Test public void fromControllerWithCustomBaseUrlViaStaticCall() { UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.org:9090/base"); UriComponents uriComponents = fromController(builder, PersonControllerImpl.class).build(); assertEquals("http://example.org:9090/base/people", uriComponents.toString()); assertEquals("http://example.org:9090/base", builder.toUriString()); }
@Test public void usesForwardedHostAsHostIfHeaderIsSet() throws Exception { this.request.addHeader("X-Forwarded-Host", "somethingDifferent"); adaptRequestFromForwardedHeaders(); UriComponents uriComponents = fromController(PersonControllerImpl.class).build(); assertThat(uriComponents.toUriString(), startsWith("http://somethingDifferent")); }
@Test public void usesFirstHostOfXForwardedHost() throws Exception { this.request.addHeader("X-Forwarded-Host", "barfoo:8888, localhost:8088"); adaptRequestFromForwardedHeaders(); UriComponents uriComponents = fromController(PersonControllerImpl.class).build(); assertThat(uriComponents.toUriString(), startsWith("http://barfoo:8888")); }
@Test public void fromControllerUriTemplate() { UriComponents uriComponents = fromController(PersonsAddressesController.class).buildAndExpand(15); assertThat(uriComponents.toUriString(), endsWith("/people/15/addresses")); }
@Test public void fromControllerWithPrefix() { initWebApplicationContext(PathPrefixWebConfig.class); this.request.setServerName("example.org"); this.request.setServerPort(9999); this.request.setContextPath("/base"); assertEquals("http://example.org:9999/base/api/people/123/addresses", fromController(PersonsAddressesController.class).buildAndExpand("123").toString()); }
/** * Create a {@link UriComponentsBuilder} from the mapping of a controller class * and current request information including Servlet mapping. If the controller * contains multiple mappings, only the first one is used. * <p><strong>Note:</strong> This method extracts values from "Forwarded" * and "X-Forwarded-*" headers if found. See class-level docs. * @param controllerType the controller to build a URI for * @return a UriComponentsBuilder instance (never {@code null}) */ public static UriComponentsBuilder fromController(Class<?> controllerType) { return fromController(null, controllerType); }
@PostMapping public ResponseEntity<PersonResource> post(@RequestBody final Person personFromRequest) { final Person person = personRepository.save(new Person(personFromRequest)); final URI uri = MvcUriComponentsBuilder.fromController(getClass()) .path("/{id}") .buildAndExpand(person.getId()) .toUri(); return ResponseEntity.created(uri).body(new PersonResource(person)); }
private URI createPostUri(final GymMembership membership) { return MvcUriComponentsBuilder.fromController(getClass()) .path("/{membershipId}") .buildAndExpand(membership.getOwner().getId(), membership.getId()) .toUri(); }