63
63
import org .springframework .web .servlet .DispatcherServlet ;
64
64
import org .springframework .web .servlet .mvc .method .RequestMappingInfoHandlerMapping ;
65
65
import org .springframework .web .servlet .support .ServletUriComponentsBuilder ;
66
- import org .springframework .web .util .UriComponents ;
67
66
import org .springframework .web .util .UriComponentsBuilder ;
68
67
69
68
/**
@@ -547,8 +546,7 @@ private static UriComponentsBuilder fromMethodInternal(@Nullable UriComponentsBu
547
546
String path = pathMatcher .combine (typePath , methodPath );
548
547
builder .path (path );
549
548
550
- UriComponents uriComponents = applyContributors (builder , method , args );
551
- return UriComponentsBuilder .newInstance ().uriComponents (uriComponents );
549
+ return applyContributors (builder , method , args );
552
550
}
553
551
554
552
private static UriComponentsBuilder getBaseUrlToUse (@ Nullable UriComponentsBuilder baseUrl ) {
@@ -626,7 +624,7 @@ else if (methods.size() > 1) {
626
624
}
627
625
}
628
626
629
- private static UriComponents applyContributors (UriComponentsBuilder builder , Method method , Object ... args ) {
627
+ private static UriComponentsBuilder applyContributors (UriComponentsBuilder builder , Method method , Object ... args ) {
630
628
CompositeUriComponentsContributor contributor = getUriComponentsContributor ();
631
629
632
630
int paramCount = method .getParameterCount ();
@@ -643,9 +641,8 @@ private static UriComponents applyContributors(UriComponentsBuilder builder, Met
643
641
contributor .contributeMethodArgument (param , args [i ], builder , uriVars );
644
642
}
645
643
646
- // We may not have all URI var values, expand only what we have
647
- return builder .build ().expand (name ->
648
- uriVars .getOrDefault (name , UriComponents .UriTemplateVariables .SKIP_VALUE ));
644
+ // This may not be all the URI variables, supply what we have so far..
645
+ return builder .uriVariables (uriVars );
649
646
}
650
647
651
648
private static CompositeUriComponentsContributor getUriComponentsContributor () {
@@ -851,7 +848,7 @@ public MethodArgumentBuilder(Class<?> controllerType, Method method) {
851
848
public MethodArgumentBuilder (@ Nullable UriComponentsBuilder baseUrl , Class <?> controllerType , Method method ) {
852
849
Assert .notNull (controllerType , "'controllerType' is required" );
853
850
Assert .notNull (method , "'method' is required" );
854
- this .baseUrl = ( baseUrl != null ? baseUrl : initBaseUrl ());
851
+ this .baseUrl = baseUrl != null ? baseUrl : UriComponentsBuilder . fromPath ( getPath ());
855
852
this .controllerType = controllerType ;
856
853
this .method = method ;
857
854
this .argumentValues = new Object [method .getParameterCount ()];
@@ -860,10 +857,10 @@ public MethodArgumentBuilder(@Nullable UriComponentsBuilder baseUrl, Class<?> co
860
857
}
861
858
}
862
859
863
- private static UriComponentsBuilder initBaseUrl () {
860
+ private static String getPath () {
864
861
UriComponentsBuilder builder = ServletUriComponentsBuilder .fromCurrentServletMapping ();
865
862
String path = builder .build ().getPath ();
866
- return ( path != null ? UriComponentsBuilder . fromPath ( path ) : UriComponentsBuilder . newInstance ()) ;
863
+ return path != null ? path : "" ;
867
864
}
868
865
869
866
public MethodArgumentBuilder arg (int index , Object value ) {
@@ -872,13 +869,13 @@ public MethodArgumentBuilder arg(int index, Object value) {
872
869
}
873
870
874
871
public String build () {
875
- return fromMethodInternal (this .baseUrl , this .controllerType , this .method ,
876
- this . argumentValues ) .build (false ).encode ().toUriString ();
872
+ return fromMethodInternal (this .baseUrl , this .controllerType , this .method , this . argumentValues )
873
+ .build (false ).encode ().toUriString ();
877
874
}
878
875
879
876
public String buildAndExpand (Object ... uriVars ) {
880
- return fromMethodInternal (this .baseUrl , this .controllerType , this .method ,
881
- this . argumentValues ) .build (false ).expand (uriVars ).encode ().toString ();
877
+ return fromMethodInternal (this .baseUrl , this .controllerType , this .method , this . argumentValues )
878
+ .build (false ).expand (uriVars ).encode ().toString ();
882
879
}
883
880
}
884
881
0 commit comments