简体   繁体   English

同一Azure应用服务中的Java API和angular SPA

[英]java API and angular SPA in the same Azure App Service

can i run, in one single app service, my java API as a virtual application under /api and my frontend angular SPA on under / ? 我可以在一个单一应用程序服务中将我的Java API作为虚拟应用程序在/ api下运行,而我的前端角度SPA在/下运行吗?

in my application settings: 在我的应用程序设置中:

  • i've set Java to version 8 我已经将Java设置为版本8
  • i've set the web container to Tomcat 9 我已将Web容器设置为Tomcat 9
  • in the virtual application section i have: 在虚拟应用程序部分中,我有:
    • virtual directory: / and path: site\\wwwroot 虚拟目录:/和路径:site \\ wwwroot
    • virtual directory: /api and path: site\\wwwroot\\api 虚拟目录:/ api和路径:site \\ wwwroot \\ api
    • both are marked as Application 两者都标记为“应用程序”

After i deploy both virtual apps, when i go to the app service URL, i get the frontend correctly, but there is also a call to the API which stays in pending for a couple of minutes and then ends in a 500 error. 部署两个虚拟应用程序后,当我转到应用程序服务URL时,可以正确获取前端,但是也有对API的调用,该API处于等待状态几分钟,然后出现500错误。 If i try to access the API directly, i get the same 500 error. 如果我尝试直接访问API,则会出现同样的500错误。

The API was built with Spring and it's web.config file i have set: 该API是使用Spring构建的,它是我设置的web.config文件:

<applicationInitialization doAppInitAfterRestart="true">
            <add initializationPage="/api"/>
            <add initializationPage="/api/health"/>
            <add initializationPage="/api/info"/>
        </applicationInitialization>

I want both, API and SPA, to be on the same domain. 我希望API和SPA都在同一个域中。 Currently, API is accessible from any domain, no restrictions on it. 目前,可以从任何域访问API,对此没有任何限制。

What am i doing wrong? 我究竟做错了什么? How can i make it work? 我该如何运作?

Per my experience, it's not necessary to create virtual directory when you deploy your web project to Azure App Service. 根据我的经验,将Web项目部署到Azure App Service时不必创建virtual directory

After you set application settings on portal, 在门户网站上设置application settings后,

在此处输入图片说明

You could put your project's generated war package under the D:\\home\\site\\wwwroot\\webapps directory on KUDU and restart your application. 您可以将项目生成的war package放在KUDU上的D:\\home\\site\\wwwroot\\webapps目录下,然后重新启动应用程序。

在此处输入图片说明

Please note that if you set the name of the war package to ROOT.war , you will be able to access your application directly through the domain name ,such as http://jaygong.azurewebsites.net/ without adding your war package name in your request url. 请注意,如果将war软件包的名称设置为ROOT.war ,则可以直接通过domain name (例如http://jaygong.azurewebsites.net/访问应用程序,而无需在其中添加war软件包的名称。您的请求网址。

You could also access static files directly through the path,such as http://jaygong.azurewebsites.net/image/image.jpg . 您也可以直接通过路径访问静态文件,例如http://jaygong.azurewebsites.net/image/image.jpg


Updated answer 更新的答案

Per my experience,you could use two ways to call another app service API in your spring boot app service. 根据我的经验,您可以使用两种方法在Spring Boot应用程序服务中调用另一个应用程序服务API。

1.The front page of the spring boot calls the background method, transfer messages between another app service API and itself via the HTTP request, and returns the result to the front end. 1. spring boot的首页调用了background方法,通过HTTP请求在另一个应用程序服务API和自身之间传递消息,并将结果返回到前端。 CORS issue does not exist in this way. 以这种方式不存在CORS问题。

2.The front page of the spring boot calls another app service API directly. 2. spring boot的首页直接调用另一个应用程序服务API。 You could refer to the js below which in this thread Enable CORS AngularJS to send HTTP POST request 您可以参考下面的js,该线程在此线程中启用CORS AngularJS发送HTTP POST请求

allowCrossDomain = function(req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
  res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
  if ('OPTIONS' === req.method) {
    res.send(200);
  } else {
    next();
  }
};

app.use(allowCrossDomain);

Hope it helps you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM