war {
enabled = true
archiveName='demo.war' // <-ここはプロジェクトに合わせて書き換えてください
}
package jp.kobespiral.hello;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloController {
@GetMapping("/hello")
public String SayHelloRequestParam(@RequestParam("name") String name, Model model) {
model.addAttribute("msg",name);
return "hello";
}
@GetMapping("/hello/{name}")
public String SayHelloPathParam(@PathVariable String name, Model model) {
model.addAttribute("msg",name);
return "hello";
}
}<!DOCTYPE html>
<html lang="ja" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Hello World</title>
</head>
<body>
This is hello.html<br>
<p th:text="${msg}"></p>
<p th:text="${msgnull}"></p>
<p>Hello [[${msg}]]. Welcome to Path Param World!</p>
<p>Hello [[${msgnull}]]. Welcome to Path Param World!</p>
</body>
</html>package jp.kobespiral.hello;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;
@RestController
@RequestMapping("/api")
public class HelloRestController {
@GetMapping("/hello")
public String SayHelloRestRequestParam(@RequestParam("name") String name) {
return "Hello " + name +". Welcome to REST Request Param API World!";
}
@GetMapping("/hello/{name}")
public String SayHelloRestPathParam(@PathVariable String name) {
return "Hello "+ name +". Welcome to REST Path Param API World!";
}
}./gradlew.bat war
2020-06-03 中村
これまではWebサービスやWebアプリの作成を想定して, warファイルを作るプロジェクトを学んできた.
この時,
$ ./gradlew.bat war
作成されるwarは,Tomcatサーバのwebappsフォルダに デプロイし運用するものである.
その一方で,Webサーバにデプロイせずにローカルの マシンで実行可能なwar(実行可能WAR)も作ることができる.
gradle でターゲットbuildを指定する.
$ ./gradlew.bat build
プロジェクトのbuild/libsに,
hogehoge-SNAPSHOT0.0.1.war
ができるので,それをターミナルからJavaで実行する
$ java -jar hogehoge-SNAPSHOT0.0.1.war
warの中にWebサーバが入っていて,
http://localhost:ポート番号/
でブラウザでアクセスできる.