Running Java application on Tomcat - java

I downloaded from github http://websystique.com/spring-boot/spring-boot-angularjs-spring-data-jpa-crud-app-example/ and import as maven and I cannot add this app to tomcat.
If i changed packaging from jar to war, I can add, but get 404 on localhost:8080/SpringBootCRUDApp
How can i run this app?

You do not need to add it to tomcat. Spring Boot uses a public static void main entry-point that launches an embedded web server for you. So just run main method.

In Your Application, you already added Springboot-starter-web Dependency in pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
And packaged your application as "jar" as below in pom.xml
<packaging>jar</packaging>
"spring -boot-starter-web" dependency has inbuilt embedded Tomcat container, so you don't need to add anything in Tomcat container
You can run your application with below command in cmd
java -jar SpringBootCRUDApplicationExample.jar

<!-- marked the embedded servlet container as provided -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
use this example for your reference.

Related

unable to deploy spring boot app on tomcat [duplicate]

This question already has answers here:
Servlet 5.0 JAR throws compile error on javax.servlet.* but Servlet 4.0 JAR does not
(3 answers)
Deploying Spring 5.x on Tomcat 10.x
(1 answer)
Closed 1 year ago.
I was trying to deploy spring boot micro-service on the tomcat server but was unable to get any response. The url was showing 404 error then I decided to go from very basic so I created a simple spring boot project with Spring Boot Initializer. There I choose
war as packaging.
java version 16 (as I only have this on my machine).
Added Spring Web dependency.
With this setting spring initializer automatically added necessary stuff like war packaging, tomcat dependency, spring web dependency and also created ServletInitializer class for me.
After that I opened the downloading project in netbeans 12 and made just a few changes i.e.
Added main class in the pom.xml file with <start-class> tag and annotated main class with #RestController and exposed 1 endpoint which return simple string.
Then from the project's main folder I ran the command mvn package also tried mvn clean install as deployment with the 1st command was unsuccessful.
Here are my configuration
Environment Variables:
System Variables
CATALINA_HOME: D:\Inzimam Tariq\apache-tomcat-10.0.10.
JAVA_HOME: C:\Program Files\Java\jdk-16.0.2
Relevent values in Path Variable: C:\Program Files\Common Files\Oracle\Java\javapath, D:\Inzimam Tariq\apache-maven-3.8.2\bin, and C:\Program Files\Java\jdk-16.0.2\bin.
User Variable
MAVEN_HOME: D:\Inzimam Tariq\apache-maven-3.8.2\bin.
I'm using windows 10 64-bit. Tomcat manager shows the app as deployed but when I click on that it shows 404. My JDK folder does not show JRE folder so I searched over internet and found that Java does not include JRE now. I also tried to rename my war file to the project name as some articles suggested that i.e. abc.war from abc-0.0.1-SNAPSHOT but still error is the same.
Please can someone point me to the right direction? Regards
After Deploying your project war file in Tomcat, try to make the following changes in your POM.xml file. Then restart your tomcat. Probably it will work.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>

Spring Initializr to create WAR for tomcat?

I would like to use the Spring Initializr to create WAR for tomcat...
Our DevOps are still not used to the idea of running java as a standalone and would like to have the application as a WAR in tomcat
I was able to produce a project but it seems like its producing a standalone spring boot application
I still want to use Spring Initializr to produce all the dependencies like :
Rest Repositories
JDBC template
Quartz
REST
One solution is to create a dynamic web project in eclipse and use Spring Initializr then just copy all the pom dependency into the dynamic web project
is there a better way?
To build a deployable war file into an external container, you have to :
Reconfigure your project to produce a WAR
Declare the embedded container ( Tomcat ) dependency as provided
<packaging>war</packaging>
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
...
</dependencies>

spring boot hot reload when jar file changed

Spring boot project, build and package a jar file then upload it to the server, run it:
java -jar xxx.jar
How can I make it auto reload when I upload a new jar file?
add Spring Boot DevTools to your dependencies for developer friendly options like auto restart of the server:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>

Spring boot embedded tomcat what is the folder structure created in deployed machine

If we run sprin-boot:run in linux
where will be the tomcat running path.
what will the folder structure of tomcat for jar and war.
will there be any work or webApps folder created?
How to find the tomcat path in linux,will it be running as separate tomcat service or the java application service.
suppose any hot fix deployment with jsp change, which was possible if there is a webapps, is it possible with spring boot?
You deploy Spring Boot as an executable JAR and run it using java -jar. It's the main class that is executed.
Tomcat or Jetty is the HTTP listener; it's running inside Spring Boot. It's the reverse of creating a WAR and deploying it to Tomcat.
There's no work or web apps folder created.
Embedded Servers in Spring boot
The idea of Embedded Server is to make the server part of the application,so in this case to be able to deploy directly to the virtual machine,you only need to have virtual machine with java already installed.
for using embedded service using tomcat you will need to have this conf in pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.0.0.RELEASE</version>
<scope>compile</scope>
</dependency>
starter-tomcat itself also has these dependecies :
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>8.5.23</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
<version>8.5.23</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-websocket</artifactId>
<version>8.5.23</version>
<scope>compile</scope>
</dependency>
Which are the tomcat dependency enough to run your application as a single jar on it's own.
Summary
in spring boot when we create an application deployable, we would embed the server inside the deployable (for example tomcat). this means, you can generate an application jar which contains Embedded Tomcat. You can run a web application as a normal Java application.as a result, When you execute mvn spring:boot run it's exactly equivalent as executing java -jar command but maven also make sure about some suble considrations:
your application are given the right parameters
making sure your application already compiled
More Info
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-embedded-web-servers.html

Google App Engine Standard env - not found controller method - Spring Boot app

I was trying to deploy Spring Boot application on Google App Engine (standard environment). At first I cloned example app from this nice tutorial https://springframework.guru/spring-boot-web-application-part-4-spring-mvc/
For example I called http://localhost:8080/products and template with data was displayed.
So everything ran without problems, I was able to call all controller methods locally. Then I decided as experiment to deploy it on GAE. I adjusted pom.xml according to instructions from here https://github.com/GoogleCloudPlatform/getting-started-java/tree/master/appengine-standard-java8/springboot-appengine-standard
It means I excluded Tomcat dependency, changed packaging from jar to war, created appengine-web.xml file etc. As next step, I created GAE project in GAE console and copied APP ID into appengine-web.xml. Then I ran mvn clean package and war was created in target folder. Finally I started with GAE deployment and it also went smoothly without errors.
My app is now deployed on this URL https://20180109t135551-dot-oe-gae-test.appspot.com/
If you try it, you will see Hello World in browser. But if I try to call /products controller method like this https://20180109t135551-dot-oe-gae-test.appspot.com/products I get "not found" error.
Can you give me advice on which URL should I call my controller methods? Did I forget to implement something like web.xml servlet mapping? Or is it some specific Spring Boot - Google App Engine problem?
I will be grateful for any hint.
Thank you all in advance
Following this steps translates into the following for the code:
In pom.xml, change <packaging>jar</packaging> to <packaging>war</packaging>
In the package guru.springframework add this class:
Code:
package guru.springframework;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class SpringBootWebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebApplication.class, args);
}
}
Remove Tomcat Starter:
Find this dependency in the POM:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
And add these lines:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
Exclude Jetty dependencies and include the Servlet API dependency:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
Add the App Engine Standard plugin:
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>1.3.1</version>
</plugin>
Add a file called appengine-web.xml in src/webapp/WEB-INF with these contents:
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<threadsafe>true</threadsafe>
<runtime>java8</runtime>
</system-properties>
</appengine-web-app>
Exclude JUL to SLF4J Bridge by locating this dependency in the pom:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
and modifying it this way:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
Avoiding out of memory errors:
In src/main/resources add a logging.properties file with:
.level = INFO
and inside src/main/webapp/WEB-INF/appengine-web.xml paste this:
<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/classes/logging.properties" />
</system-properties>
EDIT:
For steps 3 and 7 you can also go to the project explorer (in case you're using Eclipse) and navigate to Libraries -> Maven dependencies and select each library individually (jul-to-slf4j-1.7.25 and spring-boot-starter-tomcat-1.5.3.RELEASE in my case). Right click on each library and go to Maven -> Exclude Maven artifact... And click Ok. This will have the same effect on the POM as editing.

Categories

Resources