64
64
import org .springframework .web .servlet .DispatcherServlet ;
65
65
import org .springframework .web .servlet .mvc .method .RequestMappingInfoHandlerMapping ;
66
66
import org .springframework .web .servlet .support .ServletUriComponentsBuilder ;
67
- import org .springframework .web .util .UriComponents ;
68
67
import org .springframework .web .util .UriComponentsBuilder ;
69
68
70
69
/**
@@ -421,8 +420,8 @@ private static UriComponentsBuilder fromMethodInternal(@Nullable UriComponentsBu
421
420
String methodPath = getMethodRequestMapping (method );
422
421
String path = pathMatcher .combine (typePath , methodPath );
423
422
baseUrl .path (path );
424
- UriComponents uriComponents = applyContributors ( baseUrl , method , args );
425
- return UriComponentsBuilder . newInstance (). uriComponents ( uriComponents );
423
+
424
+ return applyContributors ( baseUrl , method , args );
426
425
}
427
426
428
427
private static UriComponentsBuilder getBaseUrlToUse (@ Nullable UriComponentsBuilder baseUrl ) {
@@ -487,7 +486,7 @@ else if (methods.size() > 1) {
487
486
}
488
487
}
489
488
490
- private static UriComponents applyContributors (UriComponentsBuilder builder , Method method , Object ... args ) {
489
+ private static UriComponentsBuilder applyContributors (UriComponentsBuilder builder , Method method , Object ... args ) {
491
490
CompositeUriComponentsContributor contributor = getConfiguredUriComponentsContributor ();
492
491
if (contributor == null ) {
493
492
logger .debug ("Using default CompositeUriComponentsContributor" );
@@ -508,8 +507,8 @@ private static UriComponents applyContributors(UriComponentsBuilder builder, Met
508
507
contributor .contributeMethodArgument (param , args [i ], builder , uriVars );
509
508
}
510
509
511
- // We may not have all URI var values, expand only what we have
512
- return builder .build (). expand ( name -> uriVars . getOrDefault ( name , UriComponents . UriTemplateVariables . SKIP_VALUE ) );
510
+ // This may not be all the URI variables, supply what we have so far..
511
+ return builder .uriVariables ( uriVars );
513
512
}
514
513
515
514
@ Nullable
@@ -813,7 +812,7 @@ public MethodArgumentBuilder(Class<?> controllerType, Method method) {
813
812
public MethodArgumentBuilder (@ Nullable UriComponentsBuilder baseUrl , Class <?> controllerType , Method method ) {
814
813
Assert .notNull (controllerType , "'controllerType' is required" );
815
814
Assert .notNull (method , "'method' is required" );
816
- this .baseUrl = ( baseUrl != null ? baseUrl : initBaseUrl ());
815
+ this .baseUrl = baseUrl != null ? baseUrl : UriComponentsBuilder . fromPath ( getPath ());
817
816
this .controllerType = controllerType ;
818
817
this .method = method ;
819
818
this .argumentValues = new Object [method .getParameterCount ()];
@@ -822,10 +821,10 @@ public MethodArgumentBuilder(@Nullable UriComponentsBuilder baseUrl, Class<?> co
822
821
}
823
822
}
824
823
825
- private static UriComponentsBuilder initBaseUrl () {
824
+ private static String getPath () {
826
825
UriComponentsBuilder builder = ServletUriComponentsBuilder .fromCurrentServletMapping ();
827
826
String path = builder .build ().getPath ();
828
- return ( path != null ? UriComponentsBuilder . fromPath ( path ) : UriComponentsBuilder . newInstance ()) ;
827
+ return path != null ? path : "" ;
829
828
}
830
829
831
830
public MethodArgumentBuilder arg (int index , Object value ) {
@@ -834,13 +833,13 @@ public MethodArgumentBuilder arg(int index, Object value) {
834
833
}
835
834
836
835
public String build () {
837
- return fromMethodInternal (this .baseUrl , this .controllerType , this .method ,
838
- this . argumentValues ) .build (false ).encode ().toUriString ();
836
+ return fromMethodInternal (this .baseUrl , this .controllerType , this .method , this . argumentValues )
837
+ .build (false ).encode ().toUriString ();
839
838
}
840
839
841
840
public String buildAndExpand (Object ... uriVars ) {
842
- return fromMethodInternal (this .baseUrl , this .controllerType , this .method ,
843
- this . argumentValues ) .build (false ).expand (uriVars ).encode ().toString ();
841
+ return fromMethodInternal (this .baseUrl , this .controllerType , this .method , this . argumentValues )
842
+ .build (false ).expand (uriVars ).encode ().toString ();
844
843
}
845
844
}
846
845
0 commit comments