所以我决定开始使用Undertow作为实验,并且由于它在基准测试中取得了很好的成果.虽然我认为这很棒,但有一个功能要么丢失,要么找不到.
我想开发一个RESTful Web服务,因此确定调用哪个HTTP方法对我来说很重要.现在,我可以从HttpServerExchange参数中的RequestMethod获取此内容,但是如果必须为每个处理程序而烦恼.
我的解决方案,但我知道错了,是这样的:
创建了一个名为HTTPMethod的注释接口:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface HTTPMethod {
public enum Method {
OTHER, GET, PUT, POST, DELETE
}
Method method() default Method.OTHER;
Run Code Online (Sandbox Code Playgroud)
一个"抽象"类(不是抽象的):
public abstract class RESTfulHandler implements HttpHandler {
@Override
public void handleRequest(HttpServerExchange hse) throws Exception {
for (Method method : this.getClass().getDeclaredMethods()) {
// if method is annotated with @Test
if (method.isAnnotationPresent(HTTPMethod.class)) {
Annotation annotation = method.getAnnotation(HTTPMethod.class);
HTTPMethod test = (HTTPMethod) annotation;
switch (test.method()) {
case PUT:
if (hse.getRequestMethod().toString().equals("PUT")) {
method.invoke(this);
}
break;
case POST:
if …Run Code Online (Sandbox Code Playgroud)