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
For the STS setup, I referred to My memorandum.
Create a project with Create new Spring Starter Project, The referenced libraries are as follows.
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 |
I will try it. It's done.
that's all. Until the end Thank you for reading.
Recommended Posts