Code Monkey home page Code Monkey logo

boot-restful-api-example's Introduction

HitCount

Spring Boot RESTful API - JPA Hibernate MySQL Example

by S.M.Lee(phantasmicmeans)

RESTful API using Spring Boot, Swagger2, JPA hibernate and Mysql, One to Many, Many to One bidirectional mapping

 

Relation

image

Bidirectional Mapping

  • Project - Problem (One-To-Many)

  • Problem - Project (Many-To-One)

  • Problem - SubProblem (One-To-Many)

  • SubProblem - Problem (Many-To-One)

 

Before we go, Check the domain class

1. Problem.java(part of)

@OneToMany(mappedBy = "project", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY, orphanRemoval = true)
private Set <Problem> problems = new HashSet<>();
/* Project <-> Problem One to Many bidirectional */

2. Problem.java(part of)

@ManyToOne(cascade = CascadeType.REMOVE)
@JoinColumn(name = "code", referencedColumnName = "code", nullable = false)
private Project project; 
/* Problem <-> Project  Many to One bidirectional */

@OneToMany(mappedBy = "problem", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private Set<subProblem> subProblems = new HashSet<>();
//Problem <-> SubProblem  One to Many bidirectional

3. SubProblem.java(part of)

@ManyToOne(cascade = CascadeType.REMOVE)
@JoinColumn(name = "pro_idx", referencedColumnName = "idx", nullable = false)
private Problem problem; 
/* Problem <-> Project Many to One bidirectional */

 

RESTful API Server

  1. API Description for Project

METHOD PATH DESCRIPTION
GET /api/project/{code} get Project-Problem-SubProblem with code
POST /api/project save Project (code will generate by constructor)
DELETE /api/project/{code} delete Project with code
PUT /api/project/{code} update Project with code

  2. API Description for Problem & SubProblem

METHOD PATH DESCRIPTION
GET /api/problem/{code} get all Problem-Subproblem with code
POST /api/problem/{code} save Problem with code
DELETE /api/problem/{code}/all delete all Problem-Subproblem with code
POST /api/subproblem save Subproblem

 

Curl

  1. Curl for Project

  1. Get a Project with code
curl -X GET http://localhost:8080/problem/0gn547 
  1. Save a Project with code
curl -d '{"title":"first project"}' -H "Content-Type: application/json" -X POST http://localhost:8080/project
  1. Delete a Project with code
curl -X DELETE http://localhost:8001/project/0gn547
  1. Update a Project with code
curl -X PUT -H "Content-Type: application/json; charset=utf-8" -d '{"title":"first-project-renewal"}' http://localhost:8080/project/hx6029

 

2. Curl for Problem & SubProblem  

  1. Get a Problem with code
curl -X GET http://localhost:8001/problem/0gn547 
  1. Save a Problem with code
curl -d '{"title":"first problem"}' -H "Content-Type: application/json" -X POST http://localhost:8080/problem/hx6029
  1. Delete a Problem-SubProblem with code
curl -X DELETE http://localhost:8001/problem/hx6029/all 
  1. Save a SubProblem
curl -d '{"content":"first-subproblem","pro_idx":1}' -H "Content-Type: application/json" -X POST http://localhost:8080/subproblem

 

Running the project with MySQL

append this at the end of application.yml  

spring:
    application:
      name: project-api
       
## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
    jpa: 
      properties:
        hibernate:
          dialect: org.hibernate.dialect.MySQL5InnoDBDialect
      hibernate:
        ddl-auto: update
        # Hibernate ddl auto (create, create-drop, validate, update)
      
    datasource:
      url: jdbc:mysql://{YOUR_MSQL_SERVER}:3306/{DATABASE NAME}?useSSL=false
      username: {YOUR_MYSQL_ID}
      password: {YOUR_MYSQL{PASSWORD}
      driver-class-name: com.mysql.jdbc.Driver
      hikari:
        maximum-pool-size: 2

 

Swagger

You can use the Swagger API Documentation at http://{Your_Server}:{Port}/swagger-ui.html

image

boot-restful-api-example's People

Contributors

phantasmicmeans avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.