啓動類
@SpringBootApplication
@CrossOrigin
@Controller
public class My3dApplication implements CommandLineRunner, ApplicationContextAware {
@Value("${web.upload.models}")
private String models;
private ApplicationContext context;
/**
* 完成初始化環境目錄
*
* @param args
* @throws Exception
*/
@Override
public void run(String... args) throws Exception {
// 獲取jar運行當前工作目錄
String root = System.getProperty("user.dir") + System.getProperty("file.separator") ;
// 在此完成初始化內容
// ...
}
public static void main(String[] args) {
SpringApplication.run(My3dApplication.class, args);
}
@GetMapping(value = { "/", "/#", "/index", "/home" })
public String index() {
return "index.html";
}
// 優雅地關閉當前服務
@PostMapping(value = "/shutdown")
public void shutdownContext() {
((ConfigurableApplicationContext) context).close();
}
// 獲取當前服務對象
@Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
this.context = ctx;
}
}
在前端關閉Spring Boot服務的Javascript
function shutdown() {
if(confirm(" 您確定要退出本服務?")) {
$.ajax({
url : '/shutdown',
type : 'post',
dataType : 'text',
success: null
});
setTimeout(function() { //使用 setTimeout()方法設定定時2000毫秒
window.location.reload();//頁面刷新
}, 1000);
}
return true;
}