Skip to content

Commit 100f17e

Browse files
committed
add: 为 Restful 风格的映射添加 OPTIONS 与 PATCH 注解
1 parent 84c94aa commit 100f17e

File tree

7 files changed

+155
-13
lines changed

7 files changed

+155
-13
lines changed

doc/manual/mvc/rest.man

+13-9
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
--------------------------------------------------------------------------------------
1717
如何使用 REST
1818

19-
Nutz.Mvc 对于 REST 的支持,包括4个常用方法及通用定义方法
19+
Nutz.Mvc 对于 REST 的支持,包括6个常用方法及通用定义方法
2020
* GET
2121
* POST
2222
* PUT
2323
* DELETE
24+
* OPTIONS
25+
* PATCH
2426
* @At(methods="xxx,yyy,zzz")
2527

2628
比如,你有一个 User 对象,你想为其增加 "修改" 和 "删除" 的操作,在模块类里你可以定义如下两个方法
@@ -42,7 +44,7 @@
4244
public void deleteUser(int userId){
4345
// TODO 这里是实现代码
4446
}
45-
47+
4648
// 任意自选方法
4749
@At(value="/user/?", method="fuck")
4850
public void fuckUser(int userId){
@@ -58,21 +60,23 @@
5860
关于 "路径参数" 的具体的解释,请参看 [http_adaptor.man 适配器]
5961

6062
在一个入口函数上,你可以标注一个或多个下面的注解:
61-
* {#888888;* `@GET`}
62-
* {#888888;* `@POST`}
63-
* {#888888;* `@PUT`}
64-
* {#888888;* `@DELETE`}
63+
* {#888888; @GET}
64+
* {#888888; @POST}
65+
* {#888888; @PUT}
66+
* {#888888; @DELETE}
67+
* {#888888; @OPTIONS}
68+
* {#888888; @PATCH}
6569
这几个注解描述了当前入口函数仅仅相应什么样的 HTTP 请求方法。在你的应用运行时,即使 Nut.Mvc
6670
匹配上了 URL,如果 HTTP 请求的方法和你声明的不符,它也当这个入口函数不存在。
6771

68-
如果你不声明这四个注解中的任何一个,则表示你希望你的这个入口函数处理发送到这个 URL 的任何
72+
如果你不声明这六个注解中的任何一个,则表示你希望你的这个入口函数处理发送到这个 URL 的任何
6973
HTTP 请求。
7074

71-
因此,你可以为一个入口函数声明 (@GET|@POST|@PUT|@DELETE) 中的一个或多个,处理其中一种或者多种
75+
因此,你可以为一个入口函数声明 (@GET|@POST|@PUT|@DELETE|@OPTIONS|@PATCH) 中的一个或多个,处理其中一种或者多种
7276
HTTP 请求,而另外一个入口函数不声明注解,用来处理余下的其他 HTTP 请求方法。当然,这两个入口
7377
函数的 @At 应该是一致的。
7478

75-
@since 1.r.63,标注了上述4个注解的方法,可以省略@At,此时,相当于标注了一个默认的@At
79+
@since 1.r.63,标注了上述6个注解的方法,可以省略@At,此时,相当于标注了一个默认的@At
7680

7781

7882

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.nutz.mvc.annotation;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
/**
10+
* 描述一个入口函数,是不是仅仅响应 OPTIONS 请求
11+
*
12+
* @author thomas([email protected])
13+
*/
14+
@Retention(RetentionPolicy.RUNTIME)
15+
@Target({ElementType.METHOD})
16+
@Documented
17+
public @interface OPTIONS {
18+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.nutz.mvc.annotation;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
/**
10+
* 描述一个入口函数,是不是仅仅响应 PATCH 请求
11+
*
12+
* @author thomas([email protected])
13+
*/
14+
@Retention(RetentionPolicy.RUNTIME)
15+
@Target({ElementType.METHOD})
16+
@Documented
17+
public @interface PATCH {
18+
}

src/org/nutz/mvc/impl/Loadings.java

+6
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
import org.nutz.mvc.annotation.Filters;
4343
import org.nutz.mvc.annotation.GET;
4444
import org.nutz.mvc.annotation.Modules;
45+
import org.nutz.mvc.annotation.OPTIONS;
4546
import org.nutz.mvc.annotation.Ok;
47+
import org.nutz.mvc.annotation.PATCH;
4648
import org.nutz.mvc.annotation.POST;
4749
import org.nutz.mvc.annotation.PUT;
4850
import org.nutz.mvc.annotation.PathMap;
@@ -222,6 +224,10 @@ public static void evalHttpMethod(ActionInfo ai, Method method, At at) {
222224
ai.getHttpMethods().add("PUT");
223225
if (Mirror.getAnnotationDeep(method, DELETE.class) != null)
224226
ai.getHttpMethods().add("DELETE");
227+
if (Mirror.getAnnotationDeep(method, OPTIONS.class) != null)
228+
ai.getHttpMethods().add("OPTIONS");
229+
if (Mirror.getAnnotationDeep(method, PATCH.class) != null)
230+
ai.getHttpMethods().add("PATCH");
225231
if (at != null) {
226232
for (String m : at.methods())
227233
ai.getHttpMethods().add(m.toUpperCase());

src/org/nutz/mvc/impl/NutEntryDeterminer.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import org.nutz.mvc.annotation.At;
99
import org.nutz.mvc.annotation.DELETE;
1010
import org.nutz.mvc.annotation.GET;
11+
import org.nutz.mvc.annotation.OPTIONS;
12+
import org.nutz.mvc.annotation.PATCH;
1113
import org.nutz.mvc.annotation.POST;
1214
import org.nutz.mvc.annotation.PUT;
1315

@@ -39,7 +41,14 @@ public class NutEntryDeterminer implements EntryDeterminer {
3941
public boolean isEntry(Class<?> module, Method method) {
4042
if (!Modifier.isPublic(method.getModifiers()) || method.isBridge())
4143
return false;
42-
return Mirror.isAnnotationExists(method, At.class, GET.class, POST.class, PUT.class, DELETE.class);
44+
return Mirror.isAnnotationExists(method,
45+
At.class,
46+
GET.class,
47+
POST.class,
48+
PUT.class,
49+
DELETE.class,
50+
OPTIONS.class,
51+
PATCH.class);
4352
}
4453

4554
}

test/org/nutz/mvc/init/RestModuleTest.java

+56-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package org.nutz.mvc.init;
22

3-
import static org.junit.Assert.assertEquals;
4-
53
import org.junit.Test;
64
import org.nutz.mvc.AbstractMvcTest;
75
import org.nutz.mvc.init.conf.RestModule;
86

7+
import static org.junit.Assert.assertEquals;
8+
99
public class RestModuleTest extends AbstractMvcTest {
1010

1111
protected void initServletConfig() {
@@ -89,4 +89,58 @@ public void test_pathArgs_01() throws Exception {
8989
String re = response.getAsString();
9090
assertEquals("xyz?a=45&b=23", re);
9191
}
92+
93+
@Test
94+
public void test_abc_options() throws Exception {
95+
request.setPathInfo("/abc");
96+
request.setMethod("Options");
97+
servlet.service(request, response);
98+
String re = response.getAsString();
99+
assertEquals("options", re);
100+
}
101+
102+
@Test
103+
public void test_abc_patch() throws Exception {
104+
request.setPathInfo("/abc");
105+
request.setMethod("Patch");
106+
servlet.service(request, response);
107+
String re = response.getAsString();
108+
assertEquals("patch", re);
109+
}
110+
111+
@Test
112+
public void test_oag_options() throws Exception {
113+
request.setPathInfo("/oag");
114+
request.setMethod("Options");
115+
servlet.service(request, response);
116+
String re = response.getAsString();
117+
assertEquals("options&get", re);
118+
}
119+
120+
@Test
121+
public void test_oag_get() throws Exception {
122+
request.setPathInfo("/oag");
123+
request.setMethod("Get");
124+
servlet.service(request, response);
125+
String re = response.getAsString();
126+
assertEquals("options&get", re);
127+
}
128+
129+
@Test
130+
public void test_oap_options() throws Exception {
131+
request.setPathInfo("/oap");
132+
request.setMethod("Options");
133+
servlet.service(request, response);
134+
String re = response.getAsString();
135+
assertEquals("options&post", re);
136+
}
137+
138+
@Test
139+
public void test_oap_post() throws Exception {
140+
request.setPathInfo("/oap");
141+
request.setMethod("Post");
142+
servlet.service(request, response);
143+
String re = response.getAsString();
144+
assertEquals("options&post", re);
145+
}
92146
}

test/org/nutz/mvc/init/conf/RestModule.java

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
package org.nutz.mvc.init.conf;
22

3-
import org.nutz.mvc.annotation.*;
3+
import org.nutz.mvc.annotation.At;
4+
import org.nutz.mvc.annotation.DELETE;
5+
import org.nutz.mvc.annotation.Fail;
6+
import org.nutz.mvc.annotation.GET;
7+
import org.nutz.mvc.annotation.OPTIONS;
8+
import org.nutz.mvc.annotation.Ok;
9+
import org.nutz.mvc.annotation.PATCH;
10+
import org.nutz.mvc.annotation.POST;
11+
import org.nutz.mvc.annotation.PUT;
412

513
@Ok("raw")
614
@Fail("json")
@@ -42,4 +50,29 @@ public String pathArgs_01(int a, int b, String c) {
4250
return c + "?a=" + a + "&b=" + b;
4351
}
4452

53+
@At("/abc")
54+
@OPTIONS
55+
public String options() {
56+
return "options";
57+
}
58+
59+
@At("/abc")
60+
@PATCH
61+
public String patch() {
62+
return "patch";
63+
}
64+
65+
@At("/oag")
66+
@GET
67+
@OPTIONS
68+
public String optionsAndGet() {
69+
return "options&get";
70+
}
71+
72+
@At("/oap")
73+
@POST
74+
@OPTIONS
75+
public String optionsAndPost() {
76+
return "options&post";
77+
}
4578
}

0 commit comments

Comments
 (0)