使用这些版本的库
<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
但这失败了400
http://localhost:22279/shutdown?token= “关机”
任何想法为什么?
答案 0 :(得分:0)
ShutdownHandler
的javadoc表示关闭请求是POST请求。
然后,对http://localhost:22279/shutdown?token=switchoff
的调用必须是POST
请求(也没有引号)。
另请注意,ShutdownHandler
有sendShutdown()
方法可以帮助您。
由于Jetty是一个开源项目,你甚至可以查看how sendShutdown()
is implemented。
答案 1 :(得分:0)
通过从文档中剪切出来的基本上复制/粘贴来解决它...不确定这是否是一个很好的方法,因为它有一个&#34;已经挂起的回调&#34;抱怨但它有效
所以在Jersey Context Holder中
@GET
@Path("shutdown")
@Produces(MediaType.TEXT_PLAIN)
public String shutdown(){
new Thread(new ShutDown()).start();
return "Down";
}
并且Shutdown 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);
}
}
}
非常感谢任何剪裁评论!