I want to create a Spring MVC rest application for web browser, android and ios
For web browser, I am using angular2 and my web server is apache tomcat.
So I am confused that I need to put the angular code inside my Spring mvc appllication project directory or create a separate instance for angular.If I have to create separate instance for both then how I will deploy angular code to the tomcat server.
You can do the both.
If you want to put the angular2 files to the tomcat then look here. This is might not straight forward.
If you want to deploy separately, I mean server(spring REST) and client(angular2) then angular2 app can deploy using the ng command or any web server like apache, nginx. You can look here for example
You can do that in any of the ways.
But
with a SPA + REST technology stack, I prefer separating front end and
back end with rest api because static resources are best served from front-server. Since Front-End servers (e.g. nginx,apache2) are very powerful and since you can use Cache for static resources, you can manage with a single deployment of your static resources (which should be all HTML content, JS, CSS, Images).
Related
My application has two separate parts, back-end in Java (Spring boot) and front-end in Vuejs.
I can simply deploy the JAR for my back-end code where I need to, and that's it for deployment.
But when it comes to deploying the Vuejs app, I can do something similar and just put the compiled Vuejs application in the proper path in a Java Spring boot application and that will be all for the front-end too.
It just doesn't seem right to me to put that application in Spring boot when it doesn't really have to do anything with it really other than for deployment (Maybe I know nothing like Jon Snow).
Also, when its put under a Spring boot application, manual URL editing doesn't work.
This app doesn't do anything on its own, it fetches all its data from the back-end app.
So what are my options here, can someone please guide me in the right direction?
Do I just setup a nodejs server and deploy the Vuejs app in that? I am not sure how that works, or whether should I even be doing that for a production application. And if so, where do I start with setting up nodejs?
It makes sense to deploy it together with spring, and it's very common practice, at least from my experience with Angular (which I suppose would be very similar to VueJS).
You don't need to have 2 servers running. You just let Spring server your HTML/js/CSS files, which helps you avoid any problems with CORS.
I am not really sure what 'URL manual editing', do you mean by navigating the web page by editing the URL? I don't see much use cases there tbh and I would guess that is only a matter of few settings.
In gradle - I would set up a build task (not sure if task is correct word, 2 build.gradle files, each for FE/BE, the BE would depend on FE), the FE would be run when BE is run, it creates static HTML/js (in my case from angular, but it should be similar for Vue) and BE task adds the output to the classpath of the java application so that Spring can register the HTML and serve it to you.
You could use Docker to create a Dockerized version of your Vue.js app and then you can deploy this onto a cloud service provider such as AWS (e.g. EC2).
Check out this link for a basic guide https://v2.vuejs.org/v2/cookbook/dockerize-vuejs-app.html
My approach is to deploy front-end and back-end separately.
You can use web-server to proxy requests to Vuejs or Spring boot.
For example, if you use Nginx, you can use this configuration to pass requests
# pass root request to index file
location / {
root /front_files/;
index /index.html;
}
# pass requests to static files
location ~ ^/(js|styles) {
root /front_files/;
}
# pass requests to back-end
location /api/ {
proxy_pass http://127.0.0.1:8080/;
}
The scenario is this: I'm developing a Java EE application with an Angular 2 frontend. The client has an Apache server which is usually used to serve static resources and an Oracle Weblogic for the dynamic part. The problem is that by default the Angular 2 App and the Weblogic server will not be able to talk each other due to the Same Origin Policy.
So far I have 3 possible deployment approaches in mind:
Set up a Reverse Proxy in Apache to point the REST endpoints to Weblogic
Package the Angular App in a WAR/EAR and deploy it to Weblogic. So I would end up with something like: myserver/myapp for the UI and myserver/myapp-rest for the Backend.
Package the Angular App in the same WAR as the Java backend. So I would end up with myserver/myapp for the UI and myserver/myapp/api for the REST endpoints.
There is a 4th option which would be setting up CORS, but I'm worried about the security using that approach.
Which is the right approach?
If you are allowed to make infra decisions , change apache webserver to nginx , we switched to nginx and got lot of added values in terms of concurrent processing.
In our project the angular client is served by nginx webserver which talks to java backend hosted on tomcat 8.x(our app server) , also there are couple of tiers after app-server a separate DB server and an elastic search server.
Don't feel intimidated to set up CORS, you will eventually need to allow some origins requests which don't originate on your domain and port.
If your java tech stack has spring mvc , then setting up CORS is just a matter of adding few lines of configuration. You can even hardcode your angular url to allow backend server to serve requests only from your angular URL.
In normal JavaEE world, CORS is just another filter or interceptor where you can set response headers with all the allowed origins, http methods etc. It's very simple you can look it up.
For your given choices
seems plausible and a value addition that you get is you can
delegate SSL encryption to proxy server .
seems rather odd, you would want to separate the static content server from dynamic contents server, your angular js bundles, assets
etc are mostly static, if you keep your static server separate then
you can configure cookie-less domains down the line that would make
serving a lot faster.
3 same as 2.
I would strongly suggest the CORS option , from my past experiences.
I am trying to integrate java backend with front end angular js ui.
I copied the ui code in web-inf folder and trying to show as on load of my dynamic project but I don't know how to serve static page in this web app.
Please help on it. Either I have to write something in index.html or WEB.xml file?
Well, here is my 5 cents,
Angular js is UI Framework and it should be mounted in HttpServer (node js?)
Where as Java is backend and you need to expose it as Webservice and host in web server.
I think you are using REST to consume your service in UI.
I've followed the instructions here to create the sample DukeScript "Words" app. Everything works fine. I'd now like to serve the application using a web container. Is there an example of how to package the whole thing so that it can be deployed in a web container such as Tomcat.
Ideally - what I would also like to be able to do is make code changes, deploy to Tomcat, start tomcat, fire up my browser and visit the app. i.e. follow the normal Java webapp development cycle.
Is this possible?
In this scenario Tomcat wouldn't do anything but serve static content. The words application is pure static client code (no servlets, nothing...). So you can use a simple browser to access the index.html on disk. If you insist on using tomcat for development: There's no need to redeploy. The only thing you need to do is configure tomcat to serve the static content that the build generates. If you named your application "helloworld" this would be:
/helloworld/client-web/target/helloworld-web-1.0-SNAPSHOT-bck2brwsr/public_html
Make your code changes, build, reload the page in browser and the browser will see the updates
I have a Restlet API and a GWT web application in two seperate web applications. I have added the Restlet GWT jar to the GWT web application and written proxy classes for each of the API server resources.
How do I now link up the two? Do i need to add in a servlet into web.xml?
This is probably a beginner question and I might be being very silly but this is my first GWT app.
Yes, You need to setup your Restlet Servlet in the web.xml from your GWT project.
Then, start you gwt project in debug mode (that will launch the jetty servlet) and you will have acces to http://127.0.0.1:8888/YourServlet/...
You can eventually deploy the Restlet Servlet in another server.
Finally write your client services using RequestBuilder.
Have in mind that if you dispose your Restlet Services in another host different that the one where you serves you gwt pages (eg. your restlet is accesed in http://another_host:another_port/YourServlet) then you will have to deal with SOP (http://en.wikipedia.org/wiki/Same_origin_policy).
I recommend you to have different projects one for the server and another for the gwt. The first deployed in Server1 and the second served by Server2. And then deal with SOP using a http proxy which you need to install in Server2.