0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

spring boot サンプル

Posted at

この文章は、Java spring-bootのサンプルコードです。
ビギナー向けです。
今まで、サンプルを見たり、実行したりして勉強しています。
多くのWebサイトでは、なんとなく、全コードではなく、一部だけ掲載されるケースが多かったです。多分、分かって当たり前と思って記載されていなかったかもしれません。でも、初心者にとっては、一部でも抜けると、進まなくなります。
ですので、全部の必要なコードを書きます。

要らないと思われる方は飛ばしていただいて結構です。

では、始めます。

OS:ubuntu 24.04

$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 24.04.3 LTS
Release:	24.04
Codename:	noble

Java

$ java -version
java version "25" 2025-09-16 LTS
Java(TM) SE Runtime Environment (build 25+37-LTS-3491)
Java HotSpot(TM) 64-Bit Server VM (build 25+37-LTS-3491, mixed mode, sharing)

maven

$ mvn --version
Apache Maven 3.8.7
Maven home: /usr/share/maven
Java version: 25, vendor: Oracle Corporation, runtime: /opt/java/jdk-25
Default locale: ja_JP, platform encoding: UTF-8
OS name: "linux", version: "6.14.0-32-generic", arch: "amd64", family: "unix"

任意のフォルダにプロジェクトルートフォルダを作ります。
~/java/app1 (ホームディレクトリの配下にjavaフォルダを作る。)

フォルダ構成

File: pom.xml  
Directory: 
src  
  main
    java  
      com 
        example
          demo
            DemoApplication.java

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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>springboot-azure-app</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>app1</name>
    <description>Demo Spring Boot application for Azure</description>
    <properties>
        <java.version>25</java.version>
        <maven.compiler.source>25</maven.compiler.source>
        <maven.compiler.target>25</maven.compiler.target>
    </properties>
    <dependencies>
        <!-- Spring Boot Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>3.5.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>3.5.3</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>app</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>           
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.13.0</version> <!-- 3.8.1 or higher is recommended. -->
                <configuration>
                    <source>25</source>
                    <target>25</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

DemoApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;
import java.util.Date;


@SpringBootApplication
@RestController
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @GetMapping("/")
    public String hello() {
        return "Hello, World! " + new Date();
    }
}

コンパイル

$ mvn compile

実行

$ mvn spring-boot:run

...

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v3.5.0)

...

2025-09-28T20:27:07.103+09:00  INFO 5901 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 2.837 seconds (process running for 3.226)

ブラウザで動作確認。

http://localhost:8080

Hello, World! Sun Sep 28 20:55:30 JST 2025
これが表示されたら、OKです。

ターミナルで、Ctrl + CでSpring bootを止めます。

以上です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?