Here are the steps to try out java Spring Boot on your local Mac. This time, I will summarize how to do it from the command line without using IDE etc.
https://projects.spring.io/spring-boot/
Tomcat is included as a web server, and it is a framework that allows you to easily develop web applications with java. At present, Spring Boot seems to be the best framework for java.
| version | |
|---|---|
| macOS | Sierra 10.12.3 | 
| Spring Boot | 1.5.6 | 
| java | 1.8.0_144 | 
Install from here. http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
Or at the terminal
$ java -version
If you type, a dialog will appear if java is not included, so you can move to the java page by pressing "Detailed information".

Download here.

Check if it was installed.
$ java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
Maven is a tool to download libraries. This time, I will use this to install Spring Boot.
Install Maven using brew.
$ brew update
$ brew install maven
Check if it was installed.
$ mvn --version
Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T04:39:06+09:00)
Maven home: /usr/local/Cellar/maven/3.5.0/libexec
Java version: 1.8.0_144, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre
Default locale: ja_JP, platform encoding: UTF-8
OS name: "mac os x", version: "10.12.3", arch: "x86_64", family: "mac"
Create a project with the following command.
$ mvn -B archetype:generate -DgroupId=edu.self -DartifactId=my_first_spring_boot -Dversion=1.0.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-quickstart
Various downloads start
$ cd my_first_spring_boot
Modify the generated pom.xml as follows.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>edu.self</groupId>
  <artifactId>my_first_spring_boot</artifactId>
  <packaging>jar</packaging>
  <version>1.0.0-SNAPSHOT</version>
  <name>my_first_spring_boot</name>
  <url>http://maven.apache.org</url>
  <!--Declare the use of Spring Boot and specify the version [Add]-->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
  </parent>
  <dependencies>
    <!--Specify the use of Spring Boot Web application library [Addition]-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--Specify the use of Spring Boot unit test library [Addition]-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
<!--Delete JUnit specification (because it is included in Spring Boot) [Delete]
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
 -->
  </dependencies>
  <build>
    <plugins>
      <!--Maven plugin for building Spring Boot [Addition]-->
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
  <properties>
    <!--Java version specification [Addition]-->
    <java.version>1.8</java.version>
  </properties>
</project>
Install the library mentioned earlier. If you have already installed it, you can see the list of libraries.
$ mvn dependency:tree
Modify the generated application file.
src/main/java/edu/self/App.java
package my_first_spring_boot;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController //Specifying that the class accepts web application requests
@EnableAutoConfiguration //Automatically enable various settings
public class App {
    @RequestMapping("/") //Specifying the URL path
    public String index() {
        return "Hello Spring Boot!";
    }
    public static void main(String[] args) {
      //This is the process for starting the application by Spring Boot.
      SpringApplication.run(App.class, args);
    }
}
Start with the following command.
$ mvn spring-boot:run
If the build is successful, access it with a browser and check it. http://localhost:8080/
That's it.
Recommended Posts