ShutdownHandler 在嵌入式 Jetty 中提供 400

ShutdownHandler is giving 400 in Embedded Jetty

使用了这些版本的库

    <java.version>1.8</java.version>
    <javax.websockets.version>1.1</javax.websockets.version>
    <jetty.version>9.3.8.v20160314</jetty.version>
    <jersey.version>2.22.2</jersey.version>
    <jgit.version>4.3.0.201604071810-r</jgit.version>
    <json.version>20160212</json.version>
    <junit.version>4.12</junit.version>
    <slf4j.version>1.7.12</slf4j.version>
    <maven.shade.version>2.4.1</maven.shade.version>

嵌入式Jetty就是这么用的

    Server server = new Server(Settings.PORT);
    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setDirectoriesListed(true);
    resourceHandler.setWelcomeFiles(new String[] { "./html/index.html" });
    resourceHandler.setResourceBase("./ressources/webcontent");

    ShutdownHandler shutdownHandler = new ShutdownHandler("switchoff", true, true);

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { resourceHandler, shutdownHandler, new DefaultHandler() });
    server.setHandler(handlers);

这表明 index.html

http://localhost:22279/

但这失败了 400

http://localhost:22279/shutdown?token="switchoff"

知道为什么吗?

ShutdownHandler 的 javadoc 说关闭请求是 POST 请求。

然后对 http://localhost:22279/shutdown?token=switchoff 的调用需要是 POST 请求(也没有引号)。

另请注意,ShutdownHandler 有一个 sendShutdown() 方法可以帮助您。

而且由于 Jetty 是一个开源项目,您甚至可以看看how sendShutdown() is implemented

通过基本上 copy/pastings 从文档中截取的内容解决了它...不确定这是否是一个好方法,因为它有一个 "losed with pending callback" 抱怨但它有效

所以在 Jersey 上下文持有者中

@GET
@Path("shutdown")
@Produces(MediaType.TEXT_PLAIN)
public String shutdown(){
    new Thread(new ShutDown()).start();                 
    return "Down";
}

关机 Class 看起来像那样

public class ShutDown implements Runnable{

private static final org.slf4j.Logger log = LoggerFactory.getLogger(ShutDown.class);

private static final String SHUTDOWNCOOKIE = "switchoff";

public void run() {
    try {
        URL url = new URL("http://localhost:8080/shutdown?token=" + SHUTDOWNCOOKIE);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.getResponseCode();
        String attempt = "Shutting down " + url + ": " + connection.getResponseMessage();  
        log.info(attempt);
    } catch (Exception e) {
        String error = "Error " + e.getMessage(); 
        log.debug(error);
    }
}

}

非常感谢任何剪裁评论!