A memorandum when creating a REST service with Spring Boot

Overview

I will keep a memorandum when using REST services on Spring Web using STS. Mainly for myself. Spring Web is based on Spring Framework A web application framework.

The development environment is as follows. OS : Windows 7 Home Edition 64bit Java : JavaSE 8 update 181 Spring Boot : 2.3.4 STS : 4.6.1

STS setup

For the STS setup, I referred to My memorandum.

Creating a project

Create a project with Create new Spring Starter Project, The referenced libraries are as follows.

Creating an application

The created class, configuration file, and prepared data are as follows.

Spring Boot Execution class when the application starts.

SpringWebSampleApplication.java


package jp.co.illmatics;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringWebSampleApplication {

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

}

This class is a REST API.

@RestController to make it a REST API, CORS @CrossOrigin to grant access Each was given.

The value that can be obtained is in JSON format. Details of the values will be explained later.

UsersController.java


package jp.co.illmatics.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import jp.co.illmatics.dao.UserDao;
import jp.co.illmatics.model.User;

@CrossOrigin
@RestController
public class UsersController {

	@Autowired
	private UserDao userInfoDao;

	@RequestMapping(method = RequestMethod.GET, value = "/users")
	public List<User> get() {
		return userInfoDao.getUserInfoList();
	}
}

Data Access Object. Both the interface and its implementation classes are required at the time of implementation.

UserDao.java


package jp.co.illmatics.dao;

import java.util.List;

import jp.co.illmatics.model.User;

public interface UserDao {

	public List<User> getUserInfoList();
}

UserDao implementation class. Defines a method to get a list of Users. ID column All items are acquired in ascending order.

UserDaoImpl.java


package jp.co.illmatics.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import jp.co.illmatics.model.User;

@Repository
public class UserDaoImpl implements UserDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public List<User> getUserInfoList() {
		List<User> list = jdbcTemplate.query("select * from USER_INFO ORDER BY ID"
			, new Object[] {},
			new RowMapper<User>() {
				public User mapRow(ResultSet rs, int rowNum) throws SQLException {
					User user = new User();
					user.setId(rs.getString("ID"));
					user.setName(rs.getString("NAME"));
					user.setAge(rs.getInt("AGE"));
					return user;
				}
			});
		return list;
	}
}

The model class for the USER_INFO table.

User.java


package jp.co.illmatics.model;

import javax.persistence.Table;

import org.springframework.stereotype.Component;

import lombok.Data;

@Data
@Component
@Table(name = "USER_INFO")
public class User {

	private String id;

	private String name;
	
	private Integer age;

}

DB connection information that stores test data.

application.properties


spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:XE
spring.datasource.username=xxxxxx
spring.datasource.password=xxxxxx
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
spring.jpa.show-sql=true

This is the created table.

Created table


create table USER_INFO (
  "ID" varchar2(20), 
  "NAME" varchar2(20), 
  "AGE" number(3,0), 
  constraint "PK_USER" primary key ("ID")
);

This is the data prepared in the USER_INFO table.

ID NAME AGE
user001 userName001 35
user002 userName002 30
user003 userName003 25

Operation check

I will try it. It's done. 実行結果.PNG

that's all. Until the end Thank you for reading.

Recommended Posts

A memorandum when creating a REST service with Spring Boot
A memorandum when trying Spring Data JPA with STS
Implement a simple Rest API with Spring Security with Spring Boot 2.0
Memorandum of understanding when Spring Boot 1.5.10 → Spring Boot 2.0.0
Implement a simple Rest API with Spring Security & JWT with Spring Boot 2.0
Implement a simple Web REST API server with Spring Boot + MySQL
Spring Boot Memorandum
Create a website with Spring Boot + Gradle (jdk1.8.x)
Create a simple search app with Spring Boot
Create a web api server with spring boot
Creating a common repository with Spring Data JPA
Create a Spring Boot development environment with docker
Download with Spring Boot
From creating a Spring Boot project to running an application with VS Code
[Note] Configuration file when using Logback with Spring Boot
Hello World (REST API) with Apache Camel + Spring Boot 2
A memorandum of addiction to Spring Boot2 x Doma2
[Spring Boot] Get user information with Rest API (beginner)
[JUnit 5 compatible] Write a test using JUnit 5 with Spring boot 2.2, 2.3
Customize REST API error response with Spring Boot (Part 2)
[JUnit 5] Write a validation test with Spring Boot! [Parameterization test]
Create a simple demo site with Spring Security with Spring Boot 2.1
Customize REST API error response with Spring Boot (Part 1)
I wrote a test with Spring Boot + JUnit 5 now
A memo that I was addicted to when making batch processing with Spring Boot
Error handling when the maximum file size is exceeded when uploading a file with Spring Boot
A story that stumbled when deploying a web application created with Spring Boot to EC2
Generate barcode with Spring Boot
Hello World with Spring Boot
Implement GraphQL with Spring Boot
Get started with Spring boot
Hello World with Spring Boot!
Run LIFF with Spring Boot
SNS login with Spring Boot
File upload with Spring Boot
Spring Boot starting with copy
Spring with Kotorin ―― 7. Service layer
Spring Boot starting with Docker
Hello World with Spring Boot
Set cookies with Spring Boot
Use Spring JDBC with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
Create microservices with Spring Boot
Send email with spring boot
Implement REST API with Spring Boot and JPA (Application Layer)
Implement REST API with Spring Boot and JPA (Infrastructure layer)
A story packed with the basics of Spring Boot (solved)
Let's make a simple API with EC2 + RDS + Spring boot ①
Implement REST API with Spring Boot and JPA (domain layer)
I tried Spring Boot introductory guide [Building a RESTful Web Service]
Use Basic Authentication with Spring Boot
Spring with Kotorin --4 REST API design
Let's make a book management web application with Spring Boot part1
[Rails] Avoid SELECT issued when creating a model with belongs_to defined!
gRPC on Spring Boot with grpc-spring-boot-starter
I get a 404 error when testing forms authentication with Spring Security
Set Spring profile when executing bootRun task with Spring Boot Gradle Plugin
Create an app with Spring Boot 2
Hot deploy with Spring Boot development
Database linkage with doma2 (Spring boot)