3838 *
3939 * @author Arjen Poutsma
4040 * @author Sebastien Deleuze
41+ * @author Rossen Stoyanchev
4142 * @since 5.2
4243 */
4344class RouterFunctionBuilder implements RouterFunctions .Builder {
@@ -49,191 +50,191 @@ class RouterFunctionBuilder implements RouterFunctions.Builder {
4950 private final List <HandlerFilterFunction <ServerResponse , ServerResponse >> errorHandlers = new ArrayList <>();
5051
5152
53+ @ SuppressWarnings ("unchecked" )
5254 @ Override
53- public RouterFunctions .Builder add (RouterFunction <ServerResponse > routerFunction ) {
55+ public < T extends ServerResponse > RouterFunctions .Builder add (RouterFunction <T > routerFunction ) {
5456 Assert .notNull (routerFunction , "RouterFunction must not be null" );
55- this .routerFunctions .add (routerFunction );
57+ this .routerFunctions .add (( RouterFunction < ServerResponse >) routerFunction );
5658 return this ;
5759 }
5860
59- private RouterFunctions .Builder add (RequestPredicate predicate , HandlerFunction <ServerResponse > handlerFunction ) {
60- this .routerFunctions .add (RouterFunctions .route (predicate , handlerFunction ));
61- return this ;
61+ private <T extends ServerResponse > RouterFunctions .Builder add (RequestPredicate predicate , HandlerFunction <T > handlerFunction ) {
62+ return add (RouterFunctions .route (predicate , handlerFunction ));
6263 }
6364
6465 // GET
6566
6667 @ Override
67- public RouterFunctions .Builder GET (HandlerFunction <ServerResponse > handlerFunction ) {
68+ public < T extends ServerResponse > RouterFunctions .Builder GET (HandlerFunction <T > handlerFunction ) {
6869 return add (RequestPredicates .method (HttpMethod .GET ), handlerFunction );
6970 }
7071
7172 @ Override
72- public RouterFunctions .Builder GET (RequestPredicate predicate , HandlerFunction <ServerResponse > handlerFunction ) {
73+ public < T extends ServerResponse > RouterFunctions .Builder GET (RequestPredicate predicate , HandlerFunction <T > handlerFunction ) {
7374 return add (RequestPredicates .method (HttpMethod .GET ).and (predicate ), handlerFunction );
7475 }
7576
7677 @ Override
77- public RouterFunctions .Builder GET (String pattern , HandlerFunction <ServerResponse > handlerFunction ) {
78+ public < T extends ServerResponse > RouterFunctions .Builder GET (String pattern , HandlerFunction <T > handlerFunction ) {
7879 return add (RequestPredicates .GET (pattern ), handlerFunction );
7980 }
8081
8182 @ Override
82- public RouterFunctions .Builder GET (String pattern , RequestPredicate predicate ,
83- HandlerFunction <ServerResponse > handlerFunction ) {
83+ public < T extends ServerResponse > RouterFunctions .Builder GET (String pattern , RequestPredicate predicate ,
84+ HandlerFunction <T > handlerFunction ) {
8485
8586 return add (RequestPredicates .GET (pattern ).and (predicate ), handlerFunction );
8687 }
8788
8889 // HEAD
8990
9091 @ Override
91- public RouterFunctions .Builder HEAD (HandlerFunction <ServerResponse > handlerFunction ) {
92+ public < T extends ServerResponse > RouterFunctions .Builder HEAD (HandlerFunction <T > handlerFunction ) {
9293 return add (RequestPredicates .method (HttpMethod .HEAD ), handlerFunction );
9394 }
9495
9596 @ Override
96- public RouterFunctions .Builder HEAD (RequestPredicate predicate , HandlerFunction <ServerResponse > handlerFunction ) {
97+ public < T extends ServerResponse > RouterFunctions .Builder HEAD (RequestPredicate predicate , HandlerFunction <T > handlerFunction ) {
9798 return add (RequestPredicates .method (HttpMethod .HEAD ).and (predicate ), handlerFunction );
9899 }
99100
100101 @ Override
101- public RouterFunctions .Builder HEAD (String pattern , HandlerFunction <ServerResponse > handlerFunction ) {
102+ public < T extends ServerResponse > RouterFunctions .Builder HEAD (String pattern , HandlerFunction <T > handlerFunction ) {
102103 return add (RequestPredicates .HEAD (pattern ), handlerFunction );
103104 }
104105
105106 @ Override
106- public RouterFunctions .Builder HEAD (String pattern , RequestPredicate predicate ,
107- HandlerFunction <ServerResponse > handlerFunction ) {
107+ public < T extends ServerResponse > RouterFunctions .Builder HEAD (String pattern , RequestPredicate predicate ,
108+ HandlerFunction <T > handlerFunction ) {
108109
109110 return add (RequestPredicates .HEAD (pattern ).and (predicate ), handlerFunction );
110111 }
111112
112113 // POST
113114
114115 @ Override
115- public RouterFunctions .Builder POST (HandlerFunction <ServerResponse > handlerFunction ) {
116+ public < T extends ServerResponse > RouterFunctions .Builder POST (HandlerFunction <T > handlerFunction ) {
116117 return add (RequestPredicates .method (HttpMethod .POST ), handlerFunction );
117118 }
118119
119120 @ Override
120- public RouterFunctions .Builder POST (RequestPredicate predicate , HandlerFunction <ServerResponse > handlerFunction ) {
121+ public < T extends ServerResponse > RouterFunctions .Builder POST (RequestPredicate predicate , HandlerFunction <T > handlerFunction ) {
121122 return add (RequestPredicates .method (HttpMethod .POST ).and (predicate ), handlerFunction );
122123 }
123124
124125 @ Override
125- public RouterFunctions .Builder POST (String pattern , HandlerFunction <ServerResponse > handlerFunction ) {
126+ public < T extends ServerResponse > RouterFunctions .Builder POST (String pattern , HandlerFunction <T > handlerFunction ) {
126127 return add (RequestPredicates .POST (pattern ), handlerFunction );
127128 }
128129
129130 @ Override
130- public RouterFunctions .Builder POST (String pattern , RequestPredicate predicate ,
131- HandlerFunction <ServerResponse > handlerFunction ) {
131+ public < T extends ServerResponse > RouterFunctions .Builder POST (String pattern , RequestPredicate predicate ,
132+ HandlerFunction <T > handlerFunction ) {
132133
133134 return add (RequestPredicates .POST (pattern ).and (predicate ), handlerFunction );
134135 }
135136
136137 // PUT
137138
138139 @ Override
139- public RouterFunctions .Builder PUT (HandlerFunction <ServerResponse > handlerFunction ) {
140+ public < T extends ServerResponse > RouterFunctions .Builder PUT (HandlerFunction <T > handlerFunction ) {
140141 return add (RequestPredicates .method (HttpMethod .PUT ), handlerFunction );
141142 }
142143
143144 @ Override
144- public RouterFunctions .Builder PUT (RequestPredicate predicate , HandlerFunction <ServerResponse > handlerFunction ) {
145+ public < T extends ServerResponse > RouterFunctions .Builder PUT (RequestPredicate predicate , HandlerFunction <T > handlerFunction ) {
145146 return add (RequestPredicates .method (HttpMethod .PUT ).and (predicate ), handlerFunction );
146147 }
147148
148149 @ Override
149- public RouterFunctions .Builder PUT (String pattern , HandlerFunction <ServerResponse > handlerFunction ) {
150+ public < T extends ServerResponse > RouterFunctions .Builder PUT (String pattern , HandlerFunction <T > handlerFunction ) {
150151 return add (RequestPredicates .PUT (pattern ), handlerFunction );
151152 }
152153
153154 @ Override
154- public RouterFunctions .Builder PUT (String pattern , RequestPredicate predicate ,
155- HandlerFunction <ServerResponse > handlerFunction ) {
155+ public < T extends ServerResponse > RouterFunctions .Builder PUT (String pattern , RequestPredicate predicate ,
156+ HandlerFunction <T > handlerFunction ) {
156157
157158 return add (RequestPredicates .PUT (pattern ).and (predicate ), handlerFunction );
158159 }
159160
160161 // PATCH
161162
162163 @ Override
163- public RouterFunctions .Builder PATCH (HandlerFunction <ServerResponse > handlerFunction ) {
164+ public < T extends ServerResponse > RouterFunctions .Builder PATCH (HandlerFunction <T > handlerFunction ) {
164165 return add (RequestPredicates .method (HttpMethod .PATCH ), handlerFunction );
165166 }
166167
167168 @ Override
168- public RouterFunctions .Builder PATCH (RequestPredicate predicate , HandlerFunction <ServerResponse > handlerFunction ) {
169+ public < T extends ServerResponse > RouterFunctions .Builder PATCH (RequestPredicate predicate , HandlerFunction <T > handlerFunction ) {
169170 return add (RequestPredicates .method (HttpMethod .PATCH ).and (predicate ), handlerFunction );
170171 }
171172
172173 @ Override
173- public RouterFunctions .Builder PATCH (String pattern , HandlerFunction <ServerResponse > handlerFunction ) {
174+ public < T extends ServerResponse > RouterFunctions .Builder PATCH (String pattern , HandlerFunction <T > handlerFunction ) {
174175 return add (RequestPredicates .PATCH (pattern ), handlerFunction );
175176 }
176177
177178 @ Override
178- public RouterFunctions .Builder PATCH (String pattern , RequestPredicate predicate ,
179- HandlerFunction <ServerResponse > handlerFunction ) {
179+ public < T extends ServerResponse > RouterFunctions .Builder PATCH (String pattern , RequestPredicate predicate ,
180+ HandlerFunction <T > handlerFunction ) {
180181
181182 return add (RequestPredicates .PATCH (pattern ).and (predicate ), handlerFunction );
182183 }
183184
184185 // DELETE
185186
186187 @ Override
187- public RouterFunctions .Builder DELETE (HandlerFunction <ServerResponse > handlerFunction ) {
188+ public < T extends ServerResponse > RouterFunctions .Builder DELETE (HandlerFunction <T > handlerFunction ) {
188189 return add (RequestPredicates .method (HttpMethod .DELETE ), handlerFunction );
189190 }
190191
191192 @ Override
192- public RouterFunctions .Builder DELETE (RequestPredicate predicate , HandlerFunction <ServerResponse > handlerFunction ) {
193+ public < T extends ServerResponse > RouterFunctions .Builder DELETE (RequestPredicate predicate , HandlerFunction <T > handlerFunction ) {
193194 return add (RequestPredicates .method (HttpMethod .DELETE ).and (predicate ), handlerFunction );
194195 }
195196
196197 @ Override
197- public RouterFunctions .Builder DELETE (String pattern , HandlerFunction <ServerResponse > handlerFunction ) {
198+ public < T extends ServerResponse > RouterFunctions .Builder DELETE (String pattern , HandlerFunction <T > handlerFunction ) {
198199 return add (RequestPredicates .DELETE (pattern ), handlerFunction );
199200 }
200201
201202 @ Override
202- public RouterFunctions .Builder DELETE (String pattern , RequestPredicate predicate ,
203- HandlerFunction <ServerResponse > handlerFunction ) {
203+ public < T extends ServerResponse > RouterFunctions .Builder DELETE (String pattern , RequestPredicate predicate ,
204+ HandlerFunction <T > handlerFunction ) {
204205
205206 return add (RequestPredicates .DELETE (pattern ).and (predicate ), handlerFunction );
206207 }
207208
208209 // OPTIONS
209210
210211 @ Override
211- public RouterFunctions .Builder OPTIONS (HandlerFunction <ServerResponse > handlerFunction ) {
212+ public < T extends ServerResponse > RouterFunctions .Builder OPTIONS (HandlerFunction <T > handlerFunction ) {
212213 return add (RequestPredicates .method (HttpMethod .OPTIONS ), handlerFunction );
213214 }
214215
215216 @ Override
216- public RouterFunctions .Builder OPTIONS (RequestPredicate predicate , HandlerFunction <ServerResponse > handlerFunction ) {
217+ public < T extends ServerResponse > RouterFunctions .Builder OPTIONS (RequestPredicate predicate , HandlerFunction <T > handlerFunction ) {
217218 return add (RequestPredicates .method (HttpMethod .OPTIONS ).and (predicate ), handlerFunction );
218219 }
219220
220221 @ Override
221- public RouterFunctions .Builder OPTIONS (String pattern , HandlerFunction <ServerResponse > handlerFunction ) {
222+ public < T extends ServerResponse > RouterFunctions .Builder OPTIONS (String pattern , HandlerFunction <T > handlerFunction ) {
222223 return add (RequestPredicates .OPTIONS (pattern ), handlerFunction );
223224 }
224225
225226 @ Override
226- public RouterFunctions .Builder OPTIONS (String pattern , RequestPredicate predicate ,
227- HandlerFunction <ServerResponse > handlerFunction ) {
227+ public < T extends ServerResponse > RouterFunctions .Builder OPTIONS (String pattern , RequestPredicate predicate ,
228+ HandlerFunction <T > handlerFunction ) {
228229
229230 return add (RequestPredicates .OPTIONS (pattern ).and (predicate ), handlerFunction );
230231 }
231232
232233 // other
233234
234235 @ Override
235- public RouterFunctions .Builder route (RequestPredicate predicate ,
236- HandlerFunction <ServerResponse > handlerFunction ) {
236+ public < T extends ServerResponse > RouterFunctions .Builder route (RequestPredicate predicate ,
237+ HandlerFunction <T > handlerFunction ) {
237238 return add (RouterFunctions .route (predicate , handlerFunction ));
238239 }
239240
@@ -285,13 +286,14 @@ public RouterFunctions.Builder nest(RequestPredicate predicate,
285286 return this ;
286287 }
287288
289+ @ SuppressWarnings ("unchecked" )
288290 @ Override
289- public RouterFunctions .Builder nest (RequestPredicate predicate ,
290- Supplier <RouterFunction <ServerResponse >> routerFunctionSupplier ) {
291+ public < T extends ServerResponse > RouterFunctions .Builder nest (RequestPredicate predicate ,
292+ Supplier <RouterFunction <T >> routerFunctionSupplier ) {
291293
292294 Assert .notNull (routerFunctionSupplier , "RouterFunction Supplier must not be null" );
293295
294- RouterFunction <ServerResponse > nestedRoute = routerFunctionSupplier .get ();
296+ RouterFunction <ServerResponse > nestedRoute = ( RouterFunction < ServerResponse >) routerFunctionSupplier .get ();
295297 this .routerFunctions .add (RouterFunctions .nest (predicate , nestedRoute ));
296298 return this ;
297299 }
@@ -304,17 +306,18 @@ public RouterFunctions.Builder path(String pattern,
304306 }
305307
306308 @ Override
307- public RouterFunctions .Builder path (String pattern ,
308- Supplier <RouterFunction <ServerResponse >> routerFunctionSupplier ) {
309+ public < T extends ServerResponse > RouterFunctions .Builder path (String pattern ,
310+ Supplier <RouterFunction <T >> routerFunctionSupplier ) {
309311
310312 return nest (RequestPredicates .path (pattern ), routerFunctionSupplier );
311313 }
312314
315+ @ SuppressWarnings ("unchecked" )
313316 @ Override
314- public RouterFunctions .Builder filter (HandlerFilterFunction <ServerResponse , ServerResponse > filterFunction ) {
317+ public < T extends ServerResponse , R extends ServerResponse > RouterFunctions .Builder filter (HandlerFilterFunction <T , R > filterFunction ) {
315318 Assert .notNull (filterFunction , "HandlerFilterFunction must not be null" );
316319
317- this .filterFunctions .add (filterFunction );
320+ this .filterFunctions .add (( HandlerFilterFunction < ServerResponse , ServerResponse >) filterFunction );
318321 return this ;
319322 }
320323
@@ -325,27 +328,29 @@ public RouterFunctions.Builder before(Function<ServerRequest, ServerRequest> req
325328 }
326329
327330 @ Override
328- public RouterFunctions .Builder after (
329- BiFunction <ServerRequest , ServerResponse , ServerResponse > responseProcessor ) {
331+ public < T extends ServerResponse , R extends ServerResponse > RouterFunctions .Builder after (
332+ BiFunction <ServerRequest , T , R > responseProcessor ) {
330333
331334 Assert .notNull (responseProcessor , "ResponseProcessor must not be null" );
332335 return filter (HandlerFilterFunction .ofResponseProcessor (responseProcessor ));
333336 }
334337
338+ @ SuppressWarnings ("unchecked" )
335339 @ Override
336- public RouterFunctions .Builder onError (Predicate <Throwable > predicate ,
337- BiFunction <Throwable , ServerRequest , ServerResponse > responseProvider ) {
340+ public < T extends ServerResponse > RouterFunctions .Builder onError (Predicate <Throwable > predicate ,
341+ BiFunction <Throwable , ServerRequest , T > responseProvider ) {
338342
339343 Assert .notNull (predicate , "Predicate must not be null" );
340344 Assert .notNull (responseProvider , "ResponseProvider must not be null" );
341345
342- this .errorHandlers .add (0 , HandlerFilterFunction .ofErrorHandler (predicate , responseProvider ));
346+ HandlerFilterFunction <T , T > filter = HandlerFilterFunction .ofErrorHandler (predicate , responseProvider );
347+ this .errorHandlers .add (0 , (HandlerFilterFunction <ServerResponse , ServerResponse >) filter );
343348 return this ;
344349 }
345350
346351 @ Override
347- public RouterFunctions .Builder onError (Class <? extends Throwable > exceptionType ,
348- BiFunction <Throwable , ServerRequest , ServerResponse > responseProvider ) {
352+ public < T extends ServerResponse > RouterFunctions .Builder onError (Class <? extends Throwable > exceptionType ,
353+ BiFunction <Throwable , ServerRequest , T > responseProvider ) {
349354 Assert .notNull (exceptionType , "ExceptionType must not be null" );
350355 Assert .notNull (responseProvider , "ResponseProvider must not be null" );
351356
0 commit comments