Search in sources :

Example 1 with MvcUriComponentsBuilder

use of org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder in project spring-framework by spring-projects.

the class MvcUriComponentsBuilderTests method testFromMethodNameWithCustomBaseUrlViaInstance.

@Test
public void testFromMethodNameWithCustomBaseUrlViaInstance() throws Exception {
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.org:9090/base");
    MvcUriComponentsBuilder mvcBuilder = MvcUriComponentsBuilder.relativeTo(builder);
    UriComponents uriComponents = mvcBuilder.withMethodName(ControllerWithMethods.class, "methodWithPathVariable", new Object[] { "1" }).build();
    assertEquals("http://example.org:9090/base/something/1/foo", uriComponents.toString());
    assertEquals("http://example.org:9090/base", builder.toUriString());
}
Also used : UriComponents(org.springframework.web.util.UriComponents) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) Test(org.junit.Test)

Example 2 with MvcUriComponentsBuilder

use of org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder in project spring-framework by spring-projects.

the class MvcUriComponentsBuilderTests method testFromControllerWithCustomBaseUrlViaInstance.

@Test
public void testFromControllerWithCustomBaseUrlViaInstance() {
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.org:9090/base");
    MvcUriComponentsBuilder mvcBuilder = MvcUriComponentsBuilder.relativeTo(builder);
    UriComponents uriComponents = mvcBuilder.withController(PersonControllerImpl.class).build();
    assertEquals("http://example.org:9090/base/people", uriComponents.toString());
    assertEquals("http://example.org:9090/base", builder.toUriString());
}
Also used : UriComponents(org.springframework.web.util.UriComponents) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) Test(org.junit.Test)

Example 3 with MvcUriComponentsBuilder

use of org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder in project spring-framework by spring-projects.

the class MvcUriComponentsBuilderTests method testFromMappingNameWithCustomBaseUrl.

@Test
public void testFromMappingNameWithCustomBaseUrl() throws Exception {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.setServletContext(new MockServletContext());
    context.register(WebConfig.class);
    context.refresh();
    this.request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
    UriComponentsBuilder baseUrl = UriComponentsBuilder.fromUriString("http://example.org:9999/base");
    MvcUriComponentsBuilder mvcBuilder = MvcUriComponentsBuilder.relativeTo(baseUrl);
    String url = mvcBuilder.withMappingName("PAC#getAddressesForCountry").arg(0, "DE").buildAndExpand(123);
    assertEquals("http://example.org:9999/base/people/123/addresses/DE", url);
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) Test(org.junit.Test)

Example 4 with MvcUriComponentsBuilder

use of org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder in project spring-framework by spring-projects.

the class MvcUriComponentsBuilderTests method testFromMethodCallWithCustomBaseUrlViaInstance.

@Test
public void testFromMethodCallWithCustomBaseUrlViaInstance() {
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.org:9090/base");
    MvcUriComponentsBuilder mvcBuilder = MvcUriComponentsBuilder.relativeTo(builder);
    UriComponents result = mvcBuilder.withMethodCall(on(ControllerWithMethods.class).myMethod(null)).build();
    assertEquals("http://example.org:9090/base/something/else", result.toString());
    assertEquals("http://example.org:9090/base", builder.toUriString());
}
Also used : UriComponents(org.springframework.web.util.UriComponents) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) MvcUriComponentsBuilder(org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 MvcUriComponentsBuilder (org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder)4 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)4 UriComponents (org.springframework.web.util.UriComponents)3 MockServletContext (org.springframework.mock.web.test.MockServletContext)1 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)1