File tree Expand file tree Collapse file tree 3 files changed +20
-30
lines changed
main/java/org/springframework/hateoas/mvc
test/java/org/springframework/hateoas/mvc Expand file tree Collapse file tree 3 files changed +20
-30
lines changed Original file line number Diff line number Diff line change 1717
1818import static org .springframework .util .StringUtils .*;
1919
20- import lombok .RequiredArgsConstructor ;
21- import lombok .experimental .Delegate ;
22-
2320import java .lang .reflect .Method ;
2421import java .net .URI ;
2522import java .util .Map ;
2623
2724import javax .servlet .http .HttpServletRequest ;
2825
26+ import lombok .RequiredArgsConstructor ;
27+ import lombok .experimental .Delegate ;
28+
2929import org .springframework .hateoas .Link ;
3030import org .springframework .hateoas .TemplateVariables ;
3131import org .springframework .hateoas .core .AnnotationMappingDiscoverer ;
@@ -257,11 +257,17 @@ public String toString() {
257257 * Returns a {@link UriComponentsBuilder} obtained from the current servlet mapping with scheme tweaked in case the
258258 * request contains an {@code X-Forwarded-Ssl} header, which is not (yet) supported by the underlying
259259 * {@link UriComponentsBuilder}.
260+ *
261+ * If no {@link RequestContextHolder} exists (you're outside a Spring Web call), fall back to relative URIs.
260262 *
261263 * @return
262264 */
263265 static UriComponentsBuilder getBuilder () {
264266
267+ if (RequestContextHolder .getRequestAttributes () == null ) {
268+ return UriComponentsBuilder .fromPath ("/" );
269+ }
270+
265271 HttpServletRequest request = getCurrentRequest ();
266272 UriComponentsBuilder builder = ServletUriComponentsBuilder .fromServletMapping (request );
267273
Original file line number Diff line number Diff line change 66
77import org .junit .Before ;
88import org .junit .Test ;
9+
10+ import org .springframework .hateoas .Link ;
911import org .springframework .web .context .request .RequestContextHolder ;
1012
1113/**
@@ -24,18 +26,15 @@ public void setUp() {
2426 }
2527
2628 /**
27- * @see #342
29+ * @see #408
2830 */
29- @ Test (expected = IllegalStateException .class )
30- public void createsLinkToMethodOnParameterizedControllerRoot () {
31-
32- try {
33- linkTo (methodOn (ControllerLinkBuilderUnitTest .PersonsAddressesController .class , 15 )
34- .getAddressesForCountry ("DE" )).withSelfRel ();
35- } catch (IllegalStateException e ) {
36- assertThat (e .getMessage (), equalTo ("Could not find current request via RequestContextHolder. Is this being called from a Spring MVC handler?" ));
37- throw e ;
38- }
31+ @ Test
32+ public void requestingLinkOutsideWebRequest () {
33+
34+ Link link = linkTo (methodOn (ControllerLinkBuilderUnitTest .PersonsAddressesController .class , 15 )
35+ .getAddressesForCountry ("DE" )).withSelfRel ();
36+
37+ assertThat (link , is (new Link ("/people/15/addresses/DE" ).withSelfRel ()));
3938 }
4039
4140}
Original file line number Diff line number Diff line change 2424import java .util .List ;
2525
2626import org .hamcrest .Matchers ;
27+ import org .junit .Ignore ;
2728import org .junit .Rule ;
2829import org .junit .Test ;
2930import org .junit .rules .ExpectedException ;
@@ -461,22 +462,6 @@ public void linksToMethodWithRequestParamImplicitlySetToFalse() {
461462 assertThat (link .getHref (), endsWith ("/bar" ));
462463 }
463464
464- /**
465- * @see #342
466- */
467- @ Test
468- public void mentionsRequiredUsageWithinWebRequestInException () {
469-
470- exception .expect (IllegalStateException .class );
471- exception .expectMessage ("request" );
472- exception .expectMessage ("Spring MVC" );
473-
474- RequestContextHolder .setRequestAttributes (null );
475-
476- linkTo (methodOn (ControllerLinkBuilderUnitTest .PersonsAddressesController .class , 15 ).getAddressesForCountry ("DE" ))
477- .withSelfRel ();
478- }
479-
480465 /**
481466 * @see #398
482467 */
You can’t perform that action at this time.
0 commit comments