Skip to content

A simple project demonstrating the interaction between a Python Flask REST API and a Java client. The Flask server provides a basic endpoint, and the Java client sends HTTP requests to the API. This project serves as a foundation for understanding how Python and Java can work together in applications using RESTful communication

Notifications You must be signed in to change notification settings

taoudi-abdelbasset/simple-rest-api-java-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Flask Project with Java and Python

This project demonstrates how to create a simple REST API using Flask in Python and interact with it using a Java client that sends HTTP requests.

Project Structure

.
├── app.py             # Flask API server (Python)
├── RequestSender.java # Java client to send HTTP requests
├── requirements.txt   # List of Python dependencies
├── .venv              # Virtual environment (not to be pushed to Git)
└── README.md          # Project documentation

Flask API (Python)

The Flask API (app.py) is a simple web server that listens for HTTP GET and POST requests. The API has one endpoint:

GET /api/       Returns a simple "Request received (Hello world)" message.

Running the Flask API

To run the Flask API, follow these steps:

  1. Create a Python virtual environment (if you haven't already):

    python -m venv .venv
  2. Activate the virtual environment:

    • Windows:

      .\.venv\Scripts\activate
    • Mac/Linux:

      source .venv/bin/activate
  3. Install the required dependencies:

    pip install -r requirements.txt
  4. Run the Flask app:

    python app.py
  5. The Flask app will now be running on http://0.0.0.0:8765/. You can test it by visiting http://127.0.0.1:8765/api/ in your browser or by sending an HTTP request using the Java client.

Java HTTP Requester

The Java code (RequestSender.java) sends an HTTP GET request to the Flask API and prints the response received.

Running the Java Client

  1. Compile the Java code:

    javac RequestSender.java
  2. Run the Java client:

    java RequestSender
  3. The output should display the response from the Flask API:

    Response from server: Request received (Hello world)
    

requirements.txt

This file lists all the required Python dependencies for the Flask API.

Flask==2.1.0

About

A simple project demonstrating the interaction between a Python Flask REST API and a Java client. The Flask server provides a basic endpoint, and the Java client sends HTTP requests to the API. This project serves as a foundation for understanding how Python and Java can work together in applications using RESTful communication

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published