[Spring Boot2.4.2] フォームの値のバリデーション

pom.xml

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-validation</artifactId>
		</dependency>

src/main/resources/templates/test1/index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<title>submit</title>
</head>
<body >
<form method="post" action="#" th:action="@{/test1/testform}" th:object="${test1Form}">
<p><input type="text" id="id" name="id" th:field="*{id}"/></p>
<div th:if="${#fields.hasErrors('id')}" th:errors="*{id}"></div>

<p><input type="text" id="name" name="name" th:field="*{name}"></p>
<div th:if="${#fields.hasErrors('name')}" th:errors="*{name}"></div>
<p><input type="submit" value="送信ボタン"></p>
</form>
</body>
</html>

MainController.java

package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Controller
@RequestMapping("/test1")
public class MainController {
	
    @GetMapping
    public String disp1(
    		Model model) {
    	model.addAttribute("test1Form", new Test1Form());
        return "test1/index";
    }
    @PostMapping("/testform")
	public String disp2(@Validated Test1Form test1Form
			,BindingResult br) {
		        if (br.hasErrors()) {
		        	return "test1/index";
		        }
			return "test1/testform";
		}
}

Test1Form.java

package com.example.demo;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class Test1Form {
	@NotNull(message="必須項目です")
	private Integer id;
	
	@Size(min=3, max=6, message="3文字から6文字で入力して下さい")
	private String name;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

src/main/resources/templates/test1/testform.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<title>submit</title>
</head>
<body >

<p>OK</p>

</body>
</html>

やべええええええええええええ
SpringBoot面白いかも。

[Spring Boot2.4.2] postgresのInsert

src/main/java/com.example.demo.dto
Customer.java

package com.example.demo.dto;

import javax.validation.constraints.NotNull;

public class Customer {
	@NotNull
	private String id;
	
	@NotNull
	private String username;
	
	@NotNull
	private String email;
	
	@NotNull
	private String phoneNumber;
	
	@NotNull
	private String postCode;
}

src/main/java/com.example.demo.repository
CustomerMapper.java
L @Mapperアノテーションを作る
L intは件数、insertはcreate処理
L customerは実際にinsertするobject

package com.example.demo.repository;

import com.example.demo.dto.Customer;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface CustomerMapper {
	
	int insert(Customer customer);
}

src/main/java/com.example.demo.repository
CustomerMapper.xml
L mapper namespaceでMapperを宣言
L #{fieldName} でアクセスすることができる、jdbcType= で型を指定する

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.exmaple.demo.repository.CustomerMapper">
	<insert id="insert" parameterType="com.exmaple.demo.dto.Customer">
		INSERT INTO customer VALUES (
			#{id, jdbcType=VARCHAR},
			#{username, jdbcType=VARCHAR},
			#{email, jdbcType=VARCHAR},
			#{phoneNumber, jdbcType=VARCHAR},
			#{postCode, jdbcType=VARCHAR}
		)
	</insert>
</mapper>

src/test/java/com.example.demo.config
DbConfig.java

package com.example.demo.config;

import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;

public class DbConfig {
	
	@Value("${spring.datasource.username}")
	private String username;
	
	@Value("${spring.datasource.password}")
	private String password;
	
	@Value("${spring.datasource.url}")
	private String url;
	
	@Value("${spring.datasource.driverClassName}")
	private String jdbcDriver;
	
	@Bean
	public DataSource dataSource() {
		return new TransactionAwareDataSourceProxy(
				DataSourceBuilder.create()
					.username(this.username)
					.password(this.password)
					.url(this.url)
					.driverClassName(this.jdbcDriver)
					.build());
	}
}

src/test/java/com.example.demo.service
CustomerService.java

import com.example.demo.dto.Customer;

public interface CustomerService {
	
	Customer register(Customer customer);
}

src/test/java/com.example.demo.service.impl

package com.example.demo.service.impl;

import com.example.demo.dto.Customer;
import com.example.demo.repository.CustomerMapper;
import com.example.demo.service.CustomerService;
import org.springframework.stereotype.Service;

@Service
public class CustomerServiceImpl implements CustomerService {
	
	private CustomerMapper mapper;
	
	public CustomerServiceImpl(CustomerMapper mapper) {
		this.mapper = mapper;
	}
	
	@Override
	public Customer register(Customer customer) {
		
		String formattedEmail = formatEmail(customer.getEmail());
		
		customer.setEmail(formattedEmail);
		
		mapper.insert(customer);
		return customer;
	}
	
	private String formatEmail(String email) {
		String[] separatedEmail = email.split("@");
		return separatedEmail[0] + "@" + separatedEmail[1].toLowerCase();
	}
}

src/test/java/com.example.demo.controller

package com.example.demo.controller;

import com.example.demo.dto.Customer;
import com.example.demo.service.CustomerService;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/customers")
public class CustomerController {
	
	private CustomerService customerService;
	
	public CustomerController(CustomerService customerService) {
		this.customerService = customerService;
	}
	
	@PostMapping
	public Customer post(@Validated @RequestBody Customer customer, Errors errors) {
		
		if (errors.hasErrors()) {
			throw new RuntimeException((Throwable) errors);
		}
		
		return customerService.register(customer);
	}
}

POST man
body -> json

{
    "id": "011",
    "username": "user011",
    "email": "test.user.011@EXAMPLE.com",
    "phoneNumber": "12345678901",
    "postCode": "4567123"
}

なんやろ、上手く動作しないな。全体の流れは何となく理解したが、🤮🤮🤮

org.springframework.validation.BeanPropertyBindingResult cannot be cast to class java.lang.Throwable (org.springframework.validation.BeanPropertyBindingResult is in unnamed module of loader ‘app’

[Spring Boot2.4.2] postgresのCRUDの前準備

Spring Bootのアーキテクチャ

CRUDの処理は、Mapper(Repository)クラスで行なっている。
Spring Initializrで雛形をgenerateする。
DependenciesにValidation, Spring Web, MyBatis, PostgreSQL Driveを追加する。
mybatisとは?
-> カスタムSQL、ストアドプロシージャ、高度なマッピング処理に対応

src/main/resources/application.yml

# Web
server:
  port: 8081
  servlet:
    context-path: /api

postgres/initdb/01_DDL_CREATE_TABLE.sql

CREATE TABLE customer (
	id VARCHAR(10) PRIMARY KEY,
	username VARCHAR(50) NOT NULL,
	email VARCHAR(50) NOT NULL,
	phone_number VARCHAR(11) NOT NULL,
	post_code VARCHAR(7) NOT NULL
);

postgres/initdb/02_DML_INSERT_INIT_DATA.sql

INSERT INTO customer VALUES ('001', 'user001', 'test.user.001@example.com', '12345678901', '1234567');
INSERT INTO customer VALUES ('002', 'user002', 'test.user.002@example.com', '23456789012', '2345671');
INSERT INTO customer VALUES ('003', 'user003', 'test.user.003@example.com', '34567890123', '3456712');
INSERT INTO customer VALUES ('004', 'user004', 'test.user.004@example.com', '45678901234', '4567123');
INSERT INTO customer VALUES ('005', 'user005', 'test.user.005@example.com', '56789012345', '5671234');
INSERT INTO customer VALUES ('006', 'user006', 'test.user.006@example.com', '67890123456', '6712345');
INSERT INTO customer VALUES ('007', 'user007', 'test.user.007@example.com', '78901234567', '7123456');
INSERT INTO customer VALUES ('008', 'user008', 'test.user.008@example.com', '89012345678', '1234567');
INSERT INTO customer VALUES ('009', 'user009', 'test.user.009@example.com', '90123456789', '2345671');
INSERT INTO customer VALUES ('010', 'user010', 'test.user.010@example.com', '01234567890', '3456712');

test=> select * from customer;
id | username | email | phone_number | post_code
—–+———-+—————————+————–+———–
001 | user001 | test.user.001@example.com | 12345678901 | 1234567
002 | user002 | test.user.002@example.com | 23456789012 | 2345671
003 | user003 | test.user.003@example.com | 34567890123 | 3456712
004 | user004 | test.user.004@example.com | 45678901234 | 4567123
005 | user005 | test.user.005@example.com | 56789012345 | 5671234
006 | user006 | test.user.006@example.com | 67890123456 | 6712345
007 | user007 | test.user.007@example.com | 78901234567 | 7123456
008 | user008 | test.user.008@example.com | 89012345678 | 1234567
009 | user009 | test.user.009@example.com | 90123456789 | 2345671
010 | user010 | test.user.010@example.com | 01234567890 | 3456712

application.yml
-> camel caseとsnake caseの命名の差分をMyBatis側で吸収し、適切にテーブル・カラム名とクラス・フィールド名をマッピングする。

# Datasource
spring:
  datasource:
    driverClassName: org.postgresql.Driver
    url: jdbc:postgresql://localhost:5432/test
    username: root
    password:
    
# MyBatis
mybatis:
  configuration:
    map-underscore-to-camel-case: true

com.example.demo.controllerにCustomerController.javaを作る
CustomerController.java

package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CustomerController {
	
	@GetMapping("/hello")
	public String hello() {
		return "Hello World.";
	}
}


ここまでは基礎

build.gradleに依存性を追加

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-validation'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.4'
	runtimeOnly 'org.postgresql:postgresql'
	testImplementation 'org.dbunit:dbunit:2.5.3'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

[Spring Boot2.4.2] .jarファイルにしてvagrantにデプロイしたい

STSで開発したプログラムをvagrantにデプロイしたい
-> Spring Boot Mavenプラグインを追加することで、実行可能なファイルとしてパッケージ化することができる。
-> pom.xmlに追加する

<packaging>jar</packaging>
// 省略
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

run as -> maven install

targetフォルダに demo-0.0.1-SNAPSHOT.jar が生成された

vagrantに.jarファイルを配置します

$ java -version
openjdk version “1.8.0_265”
OpenJDK Runtime Environment (build 1.8.0_265-b01)
OpenJDK 64-Bit Server VM (build 25.265-b01, mixed mode)

$ wget https://d3pxv6yz143wms.cloudfront.net/11.0.2.9.3/java-11-amazon-corretto-devel-11.0.2.9-3.x86_64.rpm
$ sudo yum localinstall java-11-amazon-corretto-devel-11.0.2.9-3.x86_64.rpm
$ java -version
openjdk version “11.0.2” 2019-01-15 LTS
OpenJDK Runtime Environment Corretto-11.0.2.9.3 (build 11.0.2+9-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.2.9.3 (build 11.0.2+9-LTS, mixed mode)
$ rm -rf java-11-amazon-corretto-devel-11.0.2.9-3.x86_64.rpm
$ java -jar demo-0.0.1-SNAPSHOT.jar

http://192.168.33.10:8080/view/what-time-is-it

うお、何これ?
サーバにJavaさえ入ってれば動くやん
Sugeeeeeeeeeeeeee
マジでビビった。

[Spring Boot2.4.2] PostgresSQLに連携したい

まず、postgres側でテーブルを作ってデータを入れます。

create table weather (
	id serial primary key,
	location_id int,
	name varchar(20),
	temperature int,
	humidity int,
	date_time timestamp
);

test=> insert into weather (location_id, name, temperature, humidity, date_time) values
(1, ‘東京’, 15, 55, ‘2021-02-02 09:00:00’),
(1, ‘東京’, 16, 53, ‘2021-02-02 10:00:00’),
(1, ‘東京’, 17, 40, ‘2021-02-02 11:00:00’),
(2, ‘那覇’, 20, 65, ‘2021-02-02 09:00:00’),
(2, ‘那覇’, 22, 67, ‘2021-02-02 10:00:00’),
(2, ‘那覇’, 25, 69, ‘2021-02-02 11:00:00’);
INSERT 0 6
test=> select * from weather;
id | location_id | name | temperature | humidity | date_time
—-+————-+——+————-+———-+———————
1 | 1 | 東京 | 15 | 55 | 2021-02-02 09:00:00
2 | 1 | 東京 | 16 | 53 | 2021-02-02 10:00:00
3 | 1 | 東京 | 17 | 40 | 2021-02-02 11:00:00
4 | 2 | 那覇 | 20 | 65 | 2021-02-02 09:00:00
5 | 2 | 那覇 | 22 | 67 | 2021-02-02 10:00:00
6 | 2 | 那覇 | 25 | 69 | 2021-02-02 11:00:00

application.properties

server.port=8080
spring.jpa.database=POSTGRESQL
spring.datasource.url=jdbc:postgresql://localhost:5432/test
spring.datasource.username=postgres
spring.datasource.password=

pom.xml

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.postgresql</groupId>
			<artifactId>postgresql</artifactId>
			<scope>runtime</scope>
		</dependency>

model
Weather.java

package com.example.demo.model;

import java.sql.Timestamp;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="weather")
public class Weather {
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id;
	
	private Integer location_id;
	
	private String name;
	
	private Integer temperature;
	
	private Integer humidity;
	
	private Timestamp date_time;
	
	public Integer getId() {
		return id;
	}
	
	public void setId(Integer id) {
		this.id = id;
	}
	
	public Integer getLocation_id() {
		return location_id;
	}
	
	public void setLocation_id(Integer location_id) {
		this.location_id = location_id;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public Integer getTemperature() {
		return temperature;
	}
	
	public void setTemperature(Integer temperature) {
		this.temperature = temperature;
	}
	
	public Integer getHumidity() {
		return humidity;
	}
	
	public void setHumidity(Integer humidity) {
		this.humidity = humidity;
	}
	
	public Timestamp getDate_time() {
		return date_time;
	}
	
	public void setDate_time(Timestamp date_time) {
		this.date_time = date_time;
	}
}

repository/WeatherRepository.java

package com.example.demo.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.example.demo.model.Weather;

@Repository
public interface WeatherRepository extends JpaRepository<Weather, Integer>{}

service/WeatherService.java

package com.example.demo.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.demo.model.Weather;
import com.example.demo.repository.WeatherRepository;

@Service
@Transactional
public class WeatherService {
	
	@Autowired
	WeatherRepository weatherRepository;
	
	public List<Weather> findAllWeatherData(){
		return weatherRepository.findAll();
	}
}

HelloController.java

package com.example.demo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.demo.model.Weather;
import com.example.demo.service.WeatherService;

@Controller
public class HelloController {
	@Autowired
	WeatherService weatherService;
	
	@RequestMapping("/hello")
	public String hello(Model model) {
		
		model.addAttribute("hello", "Hello World!");
		
		List<Weather> weatherDataList = weatherService.findAllWeatherData();
		model.addAttribute("weatherDataList", weatherDataList);
		
		return "hello";
	}
}

hello.html

<!Doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>SpringBoot</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta charset="UTF-8">
<!-- <link th:href="@{/css/common.css}" rel="stylesheet"></link>
<script th:src="@{/js/common.js}"></script> -->
</head>
<body>
<p>
	<span th:text="${hello}">Hello World!</span>
</p>
<div>
	<table>
		<tr th:each="data: ${weatherDataList}" th:object="${data}">
			<td th:text="*{id}"></td>
			<td th:text="*{location_id}"></td>
			<td th:text="*{name}"></td>
			<td th:text="*{temperature}"></td>
			<td th:text="*{humidity}"></td>
			<td th:text="*{date_time}"></td>
		</tr>
	</table>
</div>
</body>
</html>

lsof -i:8080
Run As -> SpringBoot app

http://localhost:8080/hello

Hello World!

1 1 東京 15 55 2021-02-02 09:00:00.0
2 1 東京 16 53 2021-02-02 10:00:00.0
3 1 東京 17 40 2021-02-02 11:00:00.0
4 2 那覇 20 65 2021-02-02 09:00:00.0
5 2 那覇 22 67 2021-02-02 10:00:00.0
6 2 那覇 25 69 2021-02-02 11:00:00.0

うおおおおおおおおおおおおお
マジか。。。
これ、Javaでアプリ作れんじゃん。

[Spring Boot2.4.2] Thymeleafを使おう

SpringBootのサポートするテンプレートエンジン
– Groovy, Thymeleaf, FreeMaker, Mustache

Tymeleaf利点
– 独自タグなし、ブラウザ表示

src/main/resources/templates/index.html

<!Doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>SpringBoot</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta charset="UTF-8">
<style type="text/css">
  form {
  	border-style: solid;
  	border-color: black;
  	border-width: 1px;
  	margin: 5px;
  	padding: 10px;
  }
</style>
</head>
<body>
<h1 th:text="'これはTypeleafですか?'">html-見出し1</h1>
Spring bootで推奨されています。
</body>
</html>

Run As -> SpringBoot app
http://localhost:8080/

WhatTimeIsItController.java

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@RequestMapping("view/what-time-is-it")
@Controller
public class WhatTimeIsItController {

}

– @RequestMappingを設定
– Controllerで動的な値を生成
– 設定した動的な値をThymeleafのviewで参照

what time is it

package com.example.demo;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@RequestMapping("view/what-time-is-it")
@Controller
public class WhatTimeIsItController {
	
	@GetMapping()
	public String view(Model model) {
		
		String now = LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME);
		
		model.addAttribute("datetime", now);
		return "wtii"; // viewを返す
	}

}

Run As -> SpringBoot app

やべ、だいぶわかってきた。
ちょっと理解すると、アプリ作りたい衝動が抑えられなくなってくる

MVC:ControllerがMVC:Viewにデータを渡すのにui:Modelを使用する
model.addAttribute(“”,)で値を設定している
SpEL(Spring Expression Language)とうい方式を使っている
${変数式}、*{選択変数式}、#{メッセージ式}、@{リンク式} などがある

[Spring Boot2.4.2] API取得

pom.xml

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

WebApiController.java

package com.example.demo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.apache.commons.text.StringEscapeUtils;


@RestController
@RequestMapping("/api")
public class WebApiController {
	
	@RequestMapping(value="weather/tokyo"
			, produces=MediaType.APPLICATION_JSON_VALUE
			, method=RequestMethod.GET)
	private String call() {
		RestTemplate rest = new RestTemplate();
		
		final String cityCode = "130010";
		final String endpoint = "http://weather.livedoor.com/forecast/webservice/json/v1";
	    
		final String url = endpoint + "?city=" + cityCode;
		
		ResponseEntity<String> response = rest.getForEntity(url, String.class);
		
		String json = response.getBody();
		
		return decode(json);
	}
	
	private static String decode(String string) {
		return StringEscapeUtils.unescapeJava(string);
	}
	
}

うお、Jsonとして機能してないが、やりたいことやpom.xmlの使い方、importの意味などはわかってきた。
早くデータアクセスに行きたい。

[Spring Boot2.4.2] 戻り値

String以外の戻り値にしてみる
java beanはデータを出し入れする倉庫

@RestController
@RequestMapping("/api")
public class WebApiController {
	private static final Logger log = LoggerFactory.getLogger(WebApiController.class);
	
	
	public static class HogeMogeBean {
		public HogeMogeBean(String string, int i) {
			// TODO Auto-generated constructor stub
		}
    }

    @RequestMapping("hogemoge")
    public HogeMogeBean hogemoge() {
        return new HogeMogeBean( "ほげ", 1234 );
    }
}

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Jan 31 19:21:10 JST 2021
There was an unexpected error (type=Internal Server Error, status=500).

$ curl localhost:8080/api/hogemoge
{“timestamp”:”2021-01-31T10:23:31.543+00:00″,”status”:500,”error”:”Internal Server Error”,”message”:””,”path”:”/api/hogemoge”}

なんやこれは。。
いきなりよくわからん。

[Spring Boot2.4.2] 新しいプロジェクトを作る

まず、Spring Starter Projectでプロジェクトを作ったら、packageを作ります。
com.example.demo.controller

WebApiController.java

package com.example.demo.controller;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
@RequestMapping("api")
public class WebApiController {
	
	@RequestMapping("hello")
	private String hello() {
		return "SpringBoot!";
	}
}

あれ、さっきgradleで作った時はRestControllerではなくRequestMethodだったけど、今回はRestControllerか。。ん、requestMappingってパスの事か。

application.properties

server.port=8080

Run As -> SpringBoot app
http://localhost:8080/api/hello

ほう、なるほど

パスパラメータ
-> requestMappingで仕込んで使用する

@RequestMapping("/hello/{param}")
	private String hello(@PathVariable("hoge") String param) {
		return "SpringBoot!";
	}
package com.example.demo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/sample/api")
public class WebApiController {
	private static final Logger log = LoggerFactory.getLogger(WebApiController.class);
	
	@RequestMapping("/test/{param}")
	private String testPathVariable(@PathVariable String param) {
		log.info(param);
		return "受け取ったパラメータ:" + param;
	}
	
	@RequestMapping("/test")
	private String testRequestParam(@RequestParam() String param) {
		log.info(param);
		return "受け取ったパラメータ:" + param;
	}
	
	@RequestMapping(value = "/test", method = RequestMethod.POST)
	private String testRequestBody(@RequestBody String body) {
		log.info(body);
		return "受け取ったbody:" + body;
	}
}

$ lsof -i:8080
$ kill hoge
Run As -> SpringBoot app
http://localhost:8080/sample/api/test/firstparam

なにこれ? GetParameter実装するの凄い簡単なのに、こんなにコード書かなきゃいけないの。。。

[Spring Boot] HelloWorldから始める

New Spring Starter Project

次の画面で、TymeleafとWebを選択

# MarvenとGradle
### Marven
– POM (Project Od Model) の考え方に基づく。
– ビルドファイルは、pom.xml
– プラグインによる拡張が可能

### Gradle
– AndroidStudioデフォルト
– 依存関係はGroovyに書いている

Run As -> SpringBoot app
. ____ _ __ _ _
/\\ / ___’_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
‘ |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.2)

### HTMLの作成
hello-world/src/main/resources/templates
index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>Hello SpringBoot Sample</h1>
</body>
</html>

### Controllerの作成

HelloController.java

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HelloController {
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String index(Model model) {
		return "index";
	}
}

Run As -> SpringBoot app

やべえ、なんか出来てる….