简体   繁体   English

如何在Undertow中处理HTTP方法?

[英]How do I handle HTTP Methods in Undertow?

So I've decided to start using Undertow, both as an experiment and due to the great results it achieved in benchmark tests. 所以我决定开始使用Undertow作为实验,并且由于它在基准测试中取得了很好的成果。 And while I think it's fantastic there's a feature which is either missing or I can't find. 虽然我认为这很棒,但有一个功能要么丢失,要么找不到。

I want to develop a RESTful web service so it's important for me to identify which HTTP method is being called. 我想开发一个RESTful Web服务,因此确定调用哪个HTTP方法对我来说很重要。 Now I can get this from RequestMethod in the HttpServerExchange parameter but if had to that for every handler that would become tedious. 现在,我可以从HttpServerExchange参数中的RequestMethod获取此内容,但是如果必须为每个处理程序而烦恼。

My solution, which works but I know is wrong, is this: 我的解决方案,但我知道错了,是这样的:

Created an annotation interface called HTTPMethod: 创建了一个名为HTTPMethod的注释接口:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) 
public @interface HTTPMethod {

public enum Method {

    OTHER, GET, PUT, POST, DELETE
}

Method method() default Method.OTHER;

an "abstract" class (which is not abstract): 一个“抽象”类(不是抽象的):

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 (hse.getRequestMethod().toString().equals("POST")) {
                        method.invoke(this);
                    }
                    break;

                case GET:
                    if (hse.getRequestMethod().toString().equals("GET")) {
                        method.invoke(this);
                    }
                    break;

                case DELETE:
                    if (hse.getRequestMethod().toString().equals("DELETE")) {
                        method.invoke(this);
                    }
                    break;
                case OTHER:
                    if (hse.getRequestMethod().toString().equals("OTHER")) {
                        method.invoke(this);
                    }
                    break;
            }
            if (test.method() == HTTPMethod.Method.PUT) {
                method.invoke(this);
            }
        }
    }
}

} }

and an implementation of both the above: 以及上述两者的实施:

public class ItemHandler extends RESTfulHandler{

@HTTPMethod(method=GET)
public List<String> getAllItems()
{
    System.out.println("GET");
    return new ArrayList<>();
}

@HTTPMethod(method=POST)
public void addItem()
{      
    System.out.println("POST");        
}

@HTTPMethod
public void doNothing()
{   
    System.out.println("OTHERS");      
}

} }

Now as I said, it works, but I'm sure that the abstract class and it's implementation have something missing so that they glue correctly. 现在正如我所说,它可行,但我确信抽象类和它的实现有一些缺失,以便它们正确粘合。 So my question is two fold: 所以我的问题有两个:

1) Is there a better / proper way to filter HTTP requests in Undertow? 1)是否有更好/正确的方法来过滤Undertow中的HTTP请求? 2) What is the correct way of using annotations correctly correctly in the above case? 2)在上述情况下正确使用注释的正确方法是什么?

Managed to find several answers with the help of the Redhat team and Undertow contributors, hope this helps someone else: 在Redhat团队和Undertow贡献者的帮助下管理以找到几个答案,希望这有助于其他人:

1) The latest version of Undertow has a io.undertow.server.RoutingHandler class which does the exact same thing I propose, just without the need of annotations. 1)Undertow的最新版本有一个io.undertow.server.RoutingHandler类,它完全按照我的建议完成,只需要不需要注释。

2) There's an adapter for RESTEasy by JBoss: resteasy-undertow or a custom framework hammock which includes RESTEasy + Undertow + Weld. 2)JBoss的RESTEasy适配器: resteasy-undertow或自定义框架吊床 ,包括RESTEasy + Undertow + Weld。

3) Undertow already supports Servlets 3, so they can be combined to use annotations if you prefer. 3)Undertow已经支持Servlets 3,因此如果您愿意,可以将它们组合起来使用注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM