1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
31
31
import java .nio .charset .StandardCharsets ;
32
32
import java .security .Principal ;
33
33
import java .text .SimpleDateFormat ;
34
+ import java .time .Instant ;
34
35
import java .util .ArrayList ;
35
36
import java .util .Arrays ;
37
+ import java .util .Collection ;
36
38
import java .util .Collections ;
37
39
import java .util .Date ;
38
40
import java .util .GregorianCalendar ;
44
46
import java .util .Map ;
45
47
import java .util .Optional ;
46
48
import java .util .Set ;
49
+ import java .util .UUID ;
47
50
import javax .servlet .ServletConfig ;
48
51
import javax .servlet .ServletContext ;
49
52
import javax .servlet .http .Cookie ;
@@ -1224,13 +1227,22 @@ public void bridgeMethods() throws Exception {
1224
1227
getServlet ().service (request , response );
1225
1228
}
1226
1229
1230
+ @ Test
1231
+ public void bridgeMethodsWithMultipleInterfaces () throws Exception {
1232
+ initServletWithControllers (ArticleController .class );
1233
+
1234
+ MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/method" );
1235
+ MockHttpServletResponse response = new MockHttpServletResponse ();
1236
+ getServlet ().service (request , response );
1237
+ }
1238
+
1227
1239
@ Test
1228
1240
public void requestParamMap () throws Exception {
1229
1241
initServletWithControllers (RequestParamMapController .class );
1230
1242
1231
1243
MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/map" );
1232
1244
request .addParameter ("key1" , "value1" );
1233
- request .addParameter ("key2" , new String []{"value21" , "value22" });
1245
+ request .addParameter ("key2" , new String [] {"value21" , "value22" });
1234
1246
MockHttpServletResponse response = new MockHttpServletResponse ();
1235
1247
1236
1248
getServlet ().service (request , response );
@@ -1249,7 +1261,7 @@ public void requestHeaderMap() throws Exception {
1249
1261
1250
1262
MockHttpServletRequest request = new MockHttpServletRequest ("GET" , "/map" );
1251
1263
request .addHeader ("Content-Type" , "text/html" );
1252
- request .addHeader ("Custom-Header" , new String []{"value21" , "value22" });
1264
+ request .addHeader ("Custom-Header" , new String [] {"value21" , "value22" });
1253
1265
MockHttpServletResponse response = new MockHttpServletResponse ();
1254
1266
1255
1267
getServlet ().service (request , response );
@@ -2591,7 +2603,6 @@ public View resolveViewName(final String viewName, Locale locale) throws Excepti
2591
2603
public String getContentType () {
2592
2604
return null ;
2593
2605
}
2594
-
2595
2606
@ Override
2596
2607
@ SuppressWarnings ({"unchecked" , "deprecation" , "rawtypes" })
2597
2608
public void render (@ Nullable Map model , HttpServletRequest request , HttpServletResponse response )
@@ -3106,6 +3117,78 @@ public ModelAndView method(MyEntity object) {
3106
3117
}
3107
3118
}
3108
3119
3120
+ @ RestController
3121
+ @ RequestMapping (path = ApiConstants .ARTICLES_PATH )
3122
+ public static class ArticleController implements ApiConstants , ResourceEndpoint <Article , ArticlePredicate > {
3123
+
3124
+ @ GetMapping (params = "page" )
3125
+ public Collection <Article > find (String pageable , ArticlePredicate predicate ) {
3126
+ throw new UnsupportedOperationException ("not implemented" );
3127
+ }
3128
+
3129
+ @ GetMapping
3130
+ public List <Article > find (boolean sort , ArticlePredicate predicate ) {
3131
+ throw new UnsupportedOperationException ("not implemented" );
3132
+ }
3133
+ }
3134
+
3135
+ interface ApiConstants {
3136
+
3137
+ String API_V1 = "/v1" ;
3138
+
3139
+ String ARTICLES_PATH = API_V1 + "/articles" ;
3140
+ }
3141
+
3142
+ public interface ResourceEndpoint <E extends Entity , P extends EntityPredicate > {
3143
+
3144
+ Collection <E > find (String pageable , P predicate ) throws IOException ;
3145
+
3146
+ List <E > find (boolean sort , P predicate ) throws IOException ;
3147
+ }
3148
+
3149
+ public static abstract class Entity {
3150
+
3151
+ public UUID id ;
3152
+
3153
+ public String createdBy ;
3154
+
3155
+ public Instant createdDate ;
3156
+ }
3157
+
3158
+ public static class Article extends Entity {
3159
+
3160
+ public String slug ;
3161
+
3162
+ public String title ;
3163
+
3164
+ public String content ;
3165
+ }
3166
+
3167
+ public static abstract class EntityPredicate <E extends Entity > {
3168
+
3169
+ public String createdBy ;
3170
+
3171
+ public Instant createdBefore ;
3172
+
3173
+ public Instant createdAfter ;
3174
+
3175
+ public boolean accept (E entity ) {
3176
+ return (createdBy == null || createdBy .equals (entity .createdBy )) &&
3177
+ (createdBefore == null || createdBefore .compareTo (entity .createdDate ) >= 0 ) &&
3178
+ (createdAfter == null || createdAfter .compareTo (entity .createdDate ) >= 0 );
3179
+ }
3180
+ }
3181
+
3182
+ public static class ArticlePredicate extends EntityPredicate <Article > {
3183
+
3184
+ public String query ;
3185
+
3186
+ @ Override
3187
+ public boolean accept (Article entity ) {
3188
+ return super .accept (entity ) && (query == null || (entity .title .contains (query ) || entity .content .contains (query )));
3189
+ }
3190
+ }
3191
+
3109
3192
@ Controller
3110
3193
public static class RequestParamMapController {
3111
3194
@@ -3277,7 +3360,6 @@ static class TestEntity {
3277
3360
3278
3361
private String name ;
3279
3362
3280
-
3281
3363
public String getName () {
3282
3364
return name ;
3283
3365
}
@@ -3325,16 +3407,14 @@ public void initBinder(WebDataBinder binder) {
3325
3407
}
3326
3408
3327
3409
@ RequestMapping ("/singleString" )
3328
- public void processMultipart (@ RequestParam ("content" ) String content ,
3329
- HttpServletResponse response ) throws IOException {
3330
-
3410
+ public void processMultipart (@ RequestParam ("content" ) String content , HttpServletResponse response )
3411
+ throws IOException {
3331
3412
response .getWriter ().write (content );
3332
3413
}
3333
3414
3334
3415
@ RequestMapping ("/stringArray" )
3335
- public void processMultipart (@ RequestParam ("content" ) String [] content ,
3336
- HttpServletResponse response ) throws IOException {
3337
-
3416
+ public void processMultipart (@ RequestParam ("content" ) String [] content , HttpServletResponse response )
3417
+ throws IOException {
3338
3418
response .getWriter ().write (StringUtils .arrayToDelimitedString (content , "-" ));
3339
3419
}
3340
3420
}
@@ -3458,7 +3538,7 @@ public HttpHeaders create() throws URISyntaxException {
3458
3538
3459
3539
@ RequestMapping (value = "empty" , method = RequestMethod .POST )
3460
3540
@ ResponseStatus (HttpStatus .CREATED )
3461
- public HttpHeaders createNoHeader () throws URISyntaxException {
3541
+ public HttpHeaders createNoHeader () {
3462
3542
return new HttpHeaders ();
3463
3543
}
3464
3544
}
0 commit comments