Web Page Resources Not Loading while deploying in AWS Elasticbeanstalk - java

I am running a Spring Boot Server with a React front-end. I've built the React Project to production and copied the contents of the build to the Webapp folder in my spring project. It worked perfectly when I ran it locally but when I'm uploading the WAR to AWS, the web content is showing a Whitelabel error page.
WhiteLabel Error Page when hosted in AWS
I tried changing the packaging to JAR because the AWS logs said that a JAR was being launched by the Webapp. But even with JAR packaging everything worked fine locally and not in AWS.
Below is my WebMvc Config file I'm using to direct my '/' requests to the index.html file in src/main/webapp
public class WebMvcConfig implements WebMvcConfigurer {
private final long MAX_AGE_SECS = 3600;
#Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("HEAD", "OPTIONS", "GET", "POST", "PUT", "PATCH", "DELETE")
.maxAge(MAX_AGE_SECS);
}
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/");
}
}`

Spring Boot by default allows the web files to be stored in /public or /static or /resources and not in /webapp. And these locations need not be added via config, I just needed to add an empty controller class.
I removed the addResourceHandler function, created an empty controller class
#Controller
public class MyClass{}
and moved my web files to /resources/public folder. And Spring Boot took care of the configuration.
Why it worked fine locally and did not work in AWS is still a mystery to me.
Read this quote along with the solution to my problem. Thought it was worth a share.
Quote: "The only way to have fewer bugs in your code is not to write code whenever possible. Use what is already provided, even if that takes some research, the return is worth it."

Related

Tomcat does not unpack spring boot application

thank you for your attention in reading my problem;
I'm trying to deploy a spring boot application but when I put it in the tomcat webapps folder it doesn't load my application, does anyone know what could be happening?
I already tried to run several versions of java changing the system variables, I used several versions of tomcat but none unzip the application
I'm using java version 17
and version apache-tomcat-9.0.71
Log Tomcat
Tomcat Past
I thought Spring Boot came with a pre-installed web server, so there is no need to use this method. Why don't you use the current method which supports auto deployment of your application?
An Spring Boot application is a ready to run jar file which contains tomcat/jetty and can be run with java -jar <your app>.jar.
When you want to run an application in tomcat you should build a .war file.
======================================
As found here: https://stackoverflow.com/a/27905557/2144466
Did you follow this guide: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto.traditional-deployment
and do you have a class which extends SpringBootServletInitializer andd overwrites the configure method:
`
#SpringBootApplication
public class MyApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
`
Please follow below.
Rename your war file at location webapps from web-0.0.1-SNAPSHOT.war to web.war
restart tomcat.
open url (http://localhost:8080/web) in browser.
I managed to solve the problem.
Apparently it was version incompatibility between spring, tomcat and java, by adjusting the system variables I can deploy both java 11 and java 17 applications.
Even though I managed to find a solution, I thank the friends above for dedicating their time to trying to solve my problem.

How to deploy spring boot app on external tomcat server?

I have a spring boot app. It only has few api.
Its running fine on embedded tomcat server.
Now I need to deploy to my external tomcat server.
So I added packaging as war in pom file
I tried making a war using export as war option and put this war file inside my external tomcat webapps folder and tried running it. It failed with 404 status.
War file Name
CghsMobileApp.war
My rest controller
#RestController
#RequestMapping("/cghs")
public class HcoRestController {
#Autowired
private hcoService hcoSrvc;
#GetMapping("/cghsCity")
public List<CghsCity> getCghsCity() {
return hcoSrvc.getCghsCity();
}
}
URL I tried to hit
http://localhost:8080/CghsMobileApp/cghs/cghsCity
There is no html page inside my api project. Do I need to add one for war file to work.
I am lost here.
Any help will be appriciated.
Looked at the spring boot specification, found out that in class annotated with #SpringBootApplication have to extend SpringBootServletInitializer, for external server war deployment.
package gov.cghs.CghsMobileApp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
#SpringBootApplication
public class CghsMobileAppApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(CghsMobileAppApplication.class, args);
}
}
but unfortunately only this will not help when working with Spring Profile. If any of your classes are using #Profile("profileName"), it needs to be passed in.
With the fat jar it was easy with the command line parameter --spring.profiles=<name>
When deployed in an external Tomcat that external tomcat has to to be modified (that is catalina.properties requires a -Dspring.profile=)

Spring boot not serving static files from inside jar

Spring boot is not serving static files placed inside a jar.
I've had a backend app which I decided to add frontend to. Setup tasks to copy static frontend files to src/main/resources/static. Went through a bunch of answers here on SO, all of them advise the static content (index.html, .js and .css files) should reside under src/main/resources/static or src/main/resources/public, which i both tried. I open the built .jar and the static files are there, but starting application using java -jar myApp.jar and opening localhost:8080 gives me default whitelabel error page. My application works, since i can access the api i have running on it.
Application doesn't have #EnableWebMvc or any other custom configuration.
If i manually copy/paste same static resources to project's src/main/resources/static and ran the application using #SpringBootApplication class in IDE - the resources are loaded without problem and index.html opens upon visiting localhost:8080, so it's only a problem when files are in .jar.
Should the static files be somewhere different when they're in runnable spring boot .jar file?
Spring boot version 2.1.1
I am facing the same problem.
In case it is helpfull, I can manage to serve the static files properly by adding the following configuration :
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
#Configuration
#EnableWebMvc
public class StaticFilesConfig implements WebMvcConfigurer {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}
}
... but it overrides the "implicit" auto configuration from spring boot and the rest of my filters etc. doesn't work anymore... => this is because #EnableWebMvc deactivates spring's default configuration.
[EDIT] In the end, I happened to understand that the jar containing the static files was not included in the built bootJar. You may want to check that.
HTH!

Hello. Ihave war packaging spring boot soap webservice, i deploy to tomcat but soapui request return error

I have one spring boot jar packaging helloworld soap web service and it works as jar project. But I need war file, i convert it to war packaging project and then deploy to tomcat, but when i test with soapui request return error. This is my test project link:
https://drive.google.com/open?id=1ChKcOxeOGkFpGpjYUh3FabdUFFueRWCw
I want ask, if someone have spring boot soap web service that works correctly?
i use eclipse maven project, tomcat 8.5.23 and soapui, jdk 1.8
this is some part of error text
:
HTTP Status 404 – Not Found HTTP Status 404 – Not Found /codenotfound/ws/helloworldDescription The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.Apache Tomcat/8.5.23
Add "SpringBootServletInitializer" as shown in following code to your main file. Because without SpringBootServletInitializer Tomcat will consider it as normal application it will not consider as Spring boot application
#SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer{
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoApplication .class);
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication .class, args);
}
}

Tomcat 7, Weld, RESTEasy not Scanning JARs

I have Tomcat 7.0.47 and I'm hosting a REST Easy JAXRS service. The service uses two external JARs, one that has a base repository interface and default implementation and one that creates a concrete repository derived from the base (i.e. these two JARs have a dependency).
The service works, i.e. I can send a request and get back data from the database.
Now what I'm trying to do is get the repository injected into the REST service, to do this I've changed the REST code to look like
#Path("/country")
public class CountryService {
#Inject
ICountriesRepository repository;
#GET
#Produces({"application/json", "application/xml"})
public List<Country> getCountries() throws NamingException, SQLException {
return repository.getCountries();
}
}
I've added a beans.xml file to the web application's WAR file (it's in the META-INF directory) and I've added beans.xml to both the JARS.
When I deploy the app I see the following message:
INFO: Adding scanned resource: com.mantiso.cricket.service.CountryService
but I don't see similar messages for the repository class in the JAR.
The JAR is deployed; the beans.xml file is in the JAR's META-INF directory; I've tried adding #ManagedBean to the repository class.
I'm sure I'm missing something simple, but lots of searching has turned up not a lot.
This is Tomcat 7.0.47; Weld 2.1.0; RESTEasy 3.0.5
What else should I try?
And the answer is: The beans.xml file for the web app must be in the WEB-INF directory. If it's in the META-INF directory then it's not parsed.
Although, this did appear to work OK when I tried injecting into a servlet

Categories

Resources