React.js and Tomcat - spring

I have a Spring MVC Web application which runs on Apache Tomcat Server. Someone created the front-end for this application with React.js and Redux and I need to integrate it in my project, but it is opened on port 3000 and Tomcat runs on port 8080. How can I do this? I do not know any React.

You need to know what is going on here.
React is a client-side application
If it runs on port 3000, it's probably running on webpack-dev-server
React.js doesn't run on a port. The application server runs on a port. You cannot run a React application. You can only serve an application written in react. React is a library. At the end of development, all you would get is an index.html and a (or a few) bundle.js files.
In order to serve React as a resource in your Spring MVC application, have a controller that returns React's index.html. If your react application has a react router, it will take over from there (subsequent url change in browser will not trigger server-side rendering).
My personal preference is to host react as a separate application and probably use express.js as the application server instead of tomcat.
This lengthy article also touches on how to serve React's index.html using a controller. Which is basically the same as serving your JSP or webjars

If you using spring boot in application.properties write server.port=8080. And if you using Spring you will have to change port in tomcat. In order to do so
.Go to tomcat>conf folder
.Edit server.xml
.Search "Connector port"
.Replace "8080" by your port number
.Restart tomcat server.

Related

Forwarding non-API requests to a NextJS server

I'm trying to use Spring Boot to serve a REST API and a NextJS server to serve the frontend which calls that API. Right now, I have 2 separate servers (Spring on port 80 and NextJS on port 3000). What I want to do is forward any GET requests that the spring server receives that don't start with /api/ to the NextJS server so that the Spring server would act as a proxy for those routes. I know that you can run next export and serve the assets from Spring's static folder, but I need to use getServerSideProps in NextJS so that isn't an option. How can I do this in Spring Boot?

How to run a Spring Boot Application in a real online website instead of localhost:8080

I've created an application with the spring boot framework.
Everything runs fine on the localhost:8080.
Now I want to be able to run the application from my website.
So people can log in to the app and use the application from anywhere over the internet.
What should I do to manage this?

(How) Can I run more than one Spring Boot application on the same server and port?

I have a Spring Boot web application that I'd like to split up into about six separate applications; one would provide the homepage and login at endpoints under "/", and the others would each claim a subdirectory path ("/subsystem1", "/subsystem2", etc...). I have a pretty clear idea how I could use JWT to pass authentication/authorization from the login app to the others.
The main reason for this is so that each subsystem can be modified or updated without shutting down the others. And organizationally, so we don't have to subject the entire app to a QA process when only one subsystem is changed.
Is it possible to set up multiple Spring Boot instances to run on the same server at the same time and the same port, with different paths/directories to their endpoints? How?
I was unable to find any duplicate question, but here are two related questions that may offer clues:
From Is there a standard way to customize the deploy path in Spring Boot? I learned that I can set the application property server.servlet.context-path to prefix the whole application with a subdirectory name (e.g. "/subsystem1"). But I still can't run two apps at the same time, even if both claim different subdirectories. Spring Boot reports "Web server failed to start. Port 8080 was already in use."
There's Multiple Spring-boot applications running on one Tomcat
but I'd prefer to use standalone Spring applications with their embedded Tomcat instances rather than the less-recommended WAR packaging and deployment to an external Tomcat container.
This one looks promising -- Deploying Multiple Spring Boot Web Applications in Single Server -- but the answers focus on whether standalone or Tomcat container deployments are better, and doesn't touch on the "how-to" question.
Acceptable answers:
If, as ekalin suggests, it is impossible to have multiple Spring Boot apps listen to the same port, here are a couple of ideas I have brainstormed (but don't know how to accomplish):
Perhaps the instances could be running on different ports but the main app (the one with the login page) could "forward" or redirect to the other apps in some way that hides their true URLs? E.g. "localhost:8080/subsystem1" would be an alias for "localhost:8081/".
Perhaps the applications could each have their own Docker containers, all running within a shared Docker network, and we use Docker somehow to map each URL path to the right app? Could this be set up with docker-compose?
We set up a proxy server of some kind that remaps URL paths to the separate applications.
You can't have more than one application listening on a port. How would the kernel which application to send the packages to?
You could run an http server such as nginx listening on 8080, and each application in a different port, and then proxy the requests based on the URL to the desired application.

How do I forward requests from a client to a Windows Service running on Windows Server

I have an Angular application deployed to/via IIS. This app is sending requests to REST endpoints exposed by a Spring Boot application which I am running as a Windows Service (The Spring Boot application is accessing a MongoDB but this should not be relevant, I think). The exposed REST interface is accessable via port 8080 on the servers localhost.
The Angular app is accessible as it should be but there seems to be a problem when the web app tries to access the Spring Application's REST interfaces. More specific: The client seems to be receiving HTML documents when he should be receiving JSON files.
I am new to working with IIS so beginner errors are not unlikely. I'm happy to share more information if needed!
Edit: I read again the question, and understood something wrong:
Looks like you were sending requests to your Angular server. For development you should use the embedded web server (https://docs.spring.io/spring-boot/docs/current/reference/html/howto-embedded-web-servers.html) in order to ease your server configuration steps. And from within the windows server, send request to localhost:springboot_port.
Once you want your service to be available for an external client, and to keep your service available/awake (something similar to systemd in Linux), you could try this: https://www.baeldung.com/spring-boot-app-as-a-service

How to deploy html from tomcat server with Springboot

So, the answer is probably super easy, but I just can't seem to figure it out.
I have set up a REST webservice, according to this tutorial: https://spring.io/guides/gs/rest-service/ and have set up the Requestmapping and everything using postman, and that end is working like I intended to.
I am setting up my site locally, but when I try to send a get request with jquery, I am receiving the following error: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
So I have understood that the problem is that I am trying to run my html from file:// and that the solution is to run it from localhost on the tomcat server and found this answer in another topic: Deploying just HTML, CSS webpage to Tomcat
However, I cannot find such a webapps folder, and I am assuming it is because my tomcat server is deployed using maven and springboot. So how do I deploy my html/js on the tomcat server when it is deployed this way?
I am working on a mac and with IntelliJ.
In your spring boot application you can put your index.html file in src/main/resources/static directory and it will be served by the application.
Also you may try to configure CORS in spring boot, see this answer for links.
If you are deploying normal web applications like jee apps you do it by placing your war file in webapps folder. The web apps folder is inside your tomcat
But for for intellij-idea go through this, it should work:
Where is my app placed when deploying to Tomcat?

Resources