[JAVA] [Spring Boot] Get user information with Rest API (beginner)

Things necessary

  1. entity class (Employee.java)
  2. Repository interface (EmployeeRepository.java)
  3. main class (first called class, SampleApplication.java)
  4. RestController class (Con.java)
  5. application.properties (DB information retention file)
  6. build.gradle (mainly dependencies)

Source code

Employee.java


@Entity
@Table(name="m_emp")
public class Employee {
    @Id
    @Column(name="empno")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;
    private String empname;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getEmpname() {
        return empname;
    }
    public void setEmpname(String empname) {
        this.empname = empname;
    }
}

EmployeeRepository.java


@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}

SampleApplication.java


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

Con.java


@RestController
public class Con {
    @Autowired
    EmployeeRepository empRepository;
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Model model) {
        List<Employee> emplist=empRepository.findAll();
        model.addAttribute("emplist", emplist);
        return emplist.toString();
    }
}

application.properties


spring.datasource.url=jdbc:mysql://localhost:3306/db name
spring.datasource.username=root
//No password required if no password is specified in brew install
#spring.datasource.password=mysql
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.jpa.database=MYSQL
spring.jpa.hibernate.ddl-auto=update

build.gradle


plugins {
    id 'org.springframework.boot' version '2.1.8.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
    developmentOnly
    runtimeClasspath {
        extendsFrom developmentOnly
    }
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
        compile('org.springframework.boot:spring-boot-starter-data-jpa')
        compile('org.springframework.boot:spring-boot-starter-thymeleaf')
        compile('org.springframework.boot:spring-boot-starter-web')
        runtime('org.springframework.boot:spring-boot-devtools')
        runtime('mysql:mysql-connector-java')
        compileOnly('org.projectlombok:lombok')
        implementation('org.springframework.boot:spring-boot-starter-tomcat')
        testCompile('org.springframework.boot:spring-boot-starter-test')
}

Recommended Posts

[Spring Boot] Get user information with Rest API (beginner)
[Beginner] Let's write REST API of Todo application with Spring Boot
Get started with Spring boot
Hello World (REST API) with Apache Camel + Spring Boot 2
Implement a simple Rest API with Spring Security with Spring Boot 2.0
Customize REST API error response with Spring Boot (Part 2)
Customize REST API error response with Spring Boot (Part 1)
Spring with Kotorin --4 REST API design
Get validation results with Spring Boot
Implement REST API in Spring Boot
Implement REST API with Spring Boot and JPA (Application Layer)
Implement REST API with Spring Boot and JPA (Infrastructure layer)
Implement REST API with Spring Boot and JPA (domain layer)
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
◆ Get API created by Spring Boot from React
Create a web api server with spring boot
Download with Spring Boot
Automatically map DTOs to entities with Spring Boot API
A memorandum when creating a REST service with Spring Boot
Introduce swagger-ui to REST API implemented in Spring Boot
Generate barcode with Spring Boot
Implement GraphQL with Spring Boot
I want to get the information of the class that inherits the UserDetails class of the user who is logged in 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 Boot starting with Docker
Hello World with Spring Boot
Set cookies with Spring Boot
REST API testing with REST Assured
Use Spring JDBC with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
Link API with Spring + Vue.js
Create microservices with Spring Boot
Send email with spring boot
Edit user information with Devise
Handle Java 8 date and time API with Thymeleaf with Spring Boot
Let's make a simple API with EC2 + RDS + Spring boot ①
Easily get any metrics with Spring Boot + Micrometer + Prometheus Exporter
Try hitting the zip code search API with Spring Boot
Get error information using DefaultErrorAttributes and ErrorAttributeOptions in Spring Boot 2.3
I tried to get started with Swagger using Spring Boot
Let's find out how to receive in Request Body with REST API of Spring Boot
Use Basic Authentication with Spring Boot
gRPC on Spring Boot with grpc-spring-boot-starter
Create an app with Spring Boot 2
Hot deploy with Spring Boot development
Database linkage with doma2 (Spring boot)
Spring Boot: Restful API sample project
Spring Boot programming with VS Code
Change Spring Boot REST API request / response from CamelCase to SankeCase
Until "Hello World" with Spring Boot
Inquiry application creation with Spring Boot
Get Flux result of Spring Web Flux from JS with Fetch API
spring boot access authorization RESTful API
I made a simple search form with Spring Boot + GitHub Search API.
(Intellij) Hello World with Spring Boot