Inputs on how to achieve REST based interaction between Java and Python?
<p>I have a <strong>Java</strong> process which interacts with its REST API called from my program's UI. When I receive the API call, I end up calling the (non-REST based) <strong>Python script(s)</strong> which do a bunch of work and return me back the results which are returned back as API response.
- I wanted to convert this interaction of UI API -> JAVA -> calling python scripts to become end to end a REST one, so that in coming times it becomes immaterial which language I am using instead of Python.
- Any inputs on whats the best way of making the call end-to-end a REST based ?</p>

<p>You can use Flask as a wrapper for converting your Python scripts into microservices. Then, call your Python scripts from Java by passing them a JSON string in a REST call.</p>

<p>For each python script, listen on a port between 50,000-65,000 and then send your Java-->Python REST calls to <code>http://127.0.0.1:50000</code>, <code>http://127.0.0.1:50001</code>, etc.</p>

<p>There really isn't a limit to how far you can break down your code, each function is a potential microservice. Try to encapsulate complexity in classes, create simple interfaces for these classes, and then expose these interfaces via REST. As a Java programmer I'm sure you will have no problem with this! Google "flask microservices example" for code samples to get yourself started.</p>

<p>It gets more complicated if your Java app doesn't block between calling each Python script, though. In that case you are using async calls or launching a new thread for each request.</p>

<p>(PS: There is a good article on microservices architecture <a href="https://eng.uber.com/building-tincup/" rel="nofollow">here</a>.) </p>

java python rest api