Code Monkey home page Code Monkey logo

springboot-rest-example's Introduction

springboot-example

My basic rest API example app built using Java Springboot. You can consume the api at http://localhost:8080/api/. See Consuming the API for more info.

To build and run:

You will need to install java 17 JDK, gradle, and postgres. See How to configure postgres for more info. Otherwise with docker installed you can do docker-compose up api.

Probably best to use an IDE like VSCode with the springboot extensions, Eclipse, or IntelliJ, but you can also use gradle:

./gradlew bootRun

Or, you may build the jar so you may run on any computer that has java 17. (write once, run anywhere*):

./gradlew build
java -jar build/libs/restservice-0.0.1-SNAPSHOT.jar

Running the tests

Ideally using a rich IDE, but also in the commandline using gradle:

./gradlew test

Consuming the API

To use these examples run the api then navigate to http://localhost:8080/api/. Once there you can do GET operations by changing the URL, but to do any other HTTP method you may find these examples helpful. Open the developer console and paste them and hit enter to execute.

To create a rental:

await fetch('http://127.0.0.1:8080/api/rentals/',
    {
        'method': 'POST',
        headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({owner: 'dan', description: 'A really nice house with 2 bedrooms and 1 bathroom'})
}).then(response => response.json());

To delete a rental:

let rentalToDelete = 1;
await fetch(`http://127.0.0.1:8080/api/rentals/${rentalToDelte}`,
    {
        'method': 'DELETE',
        headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    }
});

To filter rentals by description:

await fetch('http://127.0.0.1:8080/api/rentals?description=3+bed',
    {
        headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    }
}).then(response => response.json());

Configuring Postgres

WARNING: Do not use these credentials in production. Or maybe don't run this app in production at all because I have no idea if it's secure.

Also, I don't think this will be easy to set up on Windows, but you can use WSL with ubuntu installed.

With postgres installed and running:

sudo -u postgres psql -f setup-postgres.psql # must run psql as the postgres user

Now, make sure you have these environment variables set else the application won't know how to connect:

NOTE: There is probably a better way to do this, see https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config

export $(cat .example-env | xargs)

Now you can run the api server:

./gradlew bootRun

springboot-rest-example's People

Contributors

pachwenko avatar

Watchers

James Cloos avatar  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.