从容器(pythonfrontend)调用端点到简单的javabackend应用程序

2024-10-01 15:36:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的spring boot java后端应用程序,它从列表中获取元素(endpoint/get/{id}),并将元素添加到列表中(endpoint/add/{product}):

@RestController
public class DemoController {

    List<String> products = Arrays.asList("test");

    @PostMapping(path="/add/{product}")
    public int addProduct(@PathVariable final String product){
        products.add(product);
        return products.size()-1;
    }

    @GetMapping(path="/get/{id}")
    public String getValue(@PathVariable final int id){
        return products.get(id);
    }
}

作为前端,我有一个简单的python应用程序,如:

from flask import Flask, request
import requests as r
import os
app = Flask(__name__)

@app.route("/")
def renderProduct():
    return """
            <html>
                <head>
                    <title>""" + os.environ["title"] + """</title>
                </head>    
                <form id="1" method="POST">
                    <input name="getID"/>
                    <br>
                    <input name="addID">
                    <input type="submit">
                </form>
                </html>
                """

@app.route("/", methods=["POST"])
def queryAndRender():
    builded = "<html>"
    if request.form["getID"] is not None:
        resp = r.get("http://localhost:8080/get/" + request.form["getID"])
        builded = builded + "PRODUCT:" + resp.text + "<br>"

    if request.form["addID"] is not None:
        resp = r.get("http://localhost:8080/add/" + request.form["addID"])
        builded = builded + "ADDED ID:" + resp.text + "<br>"

    builded = builded + """<html>
                            <head>
                                <title>""" + os.environ["title"] + """</title>
                            </head>
                            <form id="1" method="POST">
                                <input name="getID"/>
                                <br>
                                <input name="addID">
                                <input type="submit">
                            </form>
                            </html>
                            """

    return builded;


if __name__ == "__main__":
    app.run()

和Dockerfile:

ARG version=3.8.5-alpine3.11
FROM python:${version}

ENV title="Hello world"
ENV test testspacja
ENV FLASK_APP=/main.py

RUN pip install Flask==1.1.2
RUN pip install requests==2.22.0

COPY main.py /

EXPOSE 80/tcp

ENTRYPOINT ["flask", "run"]
CMD ["-h", "0.0.0.0", "-p", "80"]

现在我可以在docker容器中运行前端:

docker run -p 8081:80 frontend

它在http://localhost:8081/下可见

现在我想在intelliJ中启动我的后端。为什么我的前端没有在端点http://localhost:8080/get/{id}http://localhost:8080/add/{product}下“看到”后端?我可以通过浏览器给他们打电话,但为什么前端不能?我只看到:

Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.  

Tags: nameformaddidlocalhosthttpinputget

热门问题