Skip to content
A starter project for spring boot api service, support both rest and graphql api. It use querydsl for dynamic sql building and aspect for authorizing graphql api.
Java TSQL Dockerfile
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.mvn/wrapper
src
.dockerignore
.gitignore
Dockerfile
README.md
docker-compose.api-test.yml
docker-compose.repository-test.yml add unit and integration tests Jan 1, 2020
docker-compose.usecase-test.yml
docker-compose.yml
mvnw
mvnw.cmd
pom.xml
settings.xml
sources.list

README.md

Spring Boot in Practice

This project can be used as a starter for spring boot api service development, supports both rest and graphql protocol. It is also a reference implementation of Clean Architecture. This api service can be used as the backend api service for this flutter app Flutter in Practice. There is also an article Spring Boot API 服务开发指南 for learning this project.

Dependent frameworks and packages

  1. Spring Boot Web framework and server
  2. Spring Data JPA Access database
  3. Querydsl JPA Type safe dynamic sql builder
  4. Spring Data Redis Cache data
  5. Spring Security Authenticate and authrorize
  6. Spring Session Manage session
  7. GraphQL Java Graphql for java
  8. GraphQL Spring Boot Starters Graphql for spring boot
  9. Flyway Database migration

APIs

Rest

Path Method Description
/user/register POST Register
/user/login POST Login
/user/logout GET Logout
/user/logged GET Get logged user
/user/modify POST Modify logged user
/user/info GET Get user info
/user/follow POST Follow user
/user/unfollow POST Unfollow user
/user/following GET Following users of someone
/user/follower GET Fans of some user
/user/sendMobileVerifyCode POST Send mobile verify code
/post/publish POST Publish post
/post/delete POST Delete post
/post/info GET Get post info
/post/published GET Get published posts of some user
/post/like POST Like post
/post/unlike POST Unlike post
/post/liked GET Liked posts of some user
/post/following GET Posts of following users of someone
/file/upload POST Upload file
/file/info GET Get file meta info

GraphQL

type Query {
    userLogout: User!
    userLogged: User
    userInfo(id: Int!): User!
    userFollowing(userId: Int, limit: Int, offset: Int): [User!]!
    userFollowingCount(userId: Int): Int!
    userFollower(userId: Int, limit: Int, offset: Int): [User!]!
    userFollowerCount(userId: Int): Int!

    postInfo(id: Int!): Post!
    postPublished(userId: Int, limit: Int, offset: Int): [Post!]!
    postPublishedCount(userId: Int): Int!
    postLiked(userId: Int, limit: Int, offset: Int): [Post!]!
    postLikedCount(userId: Int): Int!
    postFollowing(limit: Int, beforeId: Int, afterId: Int): [Post!]!
    postFollowingCount: Int!

    fileInfo(id: Int!): File!
}

type Mutation {
    userRegister(user: UserInput!): User!
    userLogin(user: UserInput!): User!
    userModify(user: UserInput!, code: String): User!
    userSendMobileVerifyCode(type: String!, mobile: String!): String!
    userFollow(userId: Int!): Boolean!
    userUnfollow(userId: Int!): Boolean!

    postPublish(post: PostInput!): Post!
    postDelete(id: Int!): Boolean!
    postLike(postId: Int!): Boolean!
    postUnlike(postId: Int!): Boolean!
}

How to run

This project need java v11+.

By local environment

Clone repository

git clone https://github.com/jaggerwang/spring-boot-in-practice.git && cd spring-boot-in-practice

Prepare mysql and redis service

Install mysql and redis server, and start them. After mysql started, create a database for this project, and a user to access this database.

CREATE DATABASE `sbip`;
CREATE USER 'sbip'@'%' IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON `sbip`.* TO 'sbip'@'%';

Configure application

Change configs in src/main/resources/application.yml as your need, especially mysql, redis and path related configs. You can also change configs by environment variables, you need add SBIP_ prefix to each config you want to change. You should make sure the directories you configured is existing.

Start server

./mvnw spring-boot:run

The running main class is net.jaggerwang.sbip.api.SbipApplication. When first start server, it will auto create tables, we use flyway to migrate database changes.

After started, the api service's endpoint is http://localhost:8080/.

By docker compose

You need install Docker and Docker Compose at first.

Configure compose

Change the content of docker-compose.yml as your need, especially the host path of mounted volumes.

Start all services

docker-compose up -d

It will start server, mysql and redis services. If you need to stop and remove all services, you can execute command docker-compose down. The container port 8080 is mapping to the same port on local host, so the endpoint of api service is same as previous.

When first start mysql, it will auto create a database sbip and a user sbip with password 123456 to access this database. The password of root user is also 123456.

How to test

By default it will not run any tests when run maven test or package task. You can specify corresponding system property to enable each test.

By local environment

Test usecases

./mvnw -Dtest.usecase.enabled=true test

Usecase tests are unit tests, it not dependent on outside mysql or redis service.

Test repositories

./mvnw -Dtest.repository.enabled=true test

Repository tests are integration tests, but it use an embedded H2 database, so there is no need to start a mysql service.

Test apis

./mvnw -Dtest.api.enabled=true test

Api tests are integration tests, it do need mysql and redis service. You can config the services for testing in application-test.yml.

Be careful, api integration tests will init and clean data in database, do not connect to the real using database.

By docker compose

To reduce the work of preparing test environment, especilly api integration tests, we can use docker container to run tests. You can use the following commands to run corresponding tests in docker container.

docker-compose -p spring-boot-in-practice-usecase-test -f docker-compose.usecase-test.yml up
docker-compose -p spring-boot-in-practice-repository-test -f docker-compose.repository-test.yml up
docker-compose -p spring-boot-in-practice-api-test -f docker-compose.api-test.yml up

After running tests, you can run the following commands to get the corresponding test result. The result of 0 means success, others means failure.

docker inspect spring-boot-in-practice-usecase-test_server_1 --format='{{.State.ExitCode}}'
docker inspect spring-boot-in-practice-repository-test_server_1 --format='{{.State.ExitCode}}'
docker inspect spring-boot-in-practice-api-test_server_1 --format='{{.State.ExitCode}}'
You can’t perform that action at this time.