Deploy Spring App to Tomcat without web.xml - java

I have successfully tested my war file with embedded TOMCAT and it works fine. Now I want to deploy it to TOMCAT but I keep getting a 404 error.
The URL I navigate to on the browser is
http://localhost:9999/springrest/demo/users/all
My TOMCAT uses the port 9999
My JRE_HOME version is 1.8.0_261
My pom.xml has <java.version>1.8</java.version>
TOMCAT manager shows the app as running
My Controller code is as follows
#RestController
#RequestMapping(path="/demo")
public class UserController {
#Autowired
private UserRepository userRepository;
#GetMapping(path="/users/all")
public #ResponseBody Iterable<User> getAllUsers() {
// This returns a JSON or XML with the users
return userRepository.findAll();
}
}
My pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.eptc</groupId>
<artifactId>springrest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springrest</name>
<description>Rest API for mobile app</description>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
<start-class>com.eptc.springrest.SpringrestApplication</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.2</version>
<scope>test</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

Have you initialized the Servlet context required by Tomcat by implementing the SpringBootServletInitializer interface?
#SpringBootApplication
public class SpringBootTomcatApplication extends SpringBootServletInitializer {
}
Please refer this link for the details :Deploy a Spring Boot WAR into a Tomcat Server

Changed my Apache Tomcat version to Apache Tomcat 8.5.58 and it worked like a charm.
The Springboot banner even shows on cmd on running catalina.bat run

Related

Deploy Springboot WAR to Tomcat 8.5 shows blank page

I can deploy / undeploy the Springboot WAR package to Tomcat 8.5 successfully using the tomcat manager,
but when I try to open the webpage it shows only blank white page.
what do I miss?
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>myapplication</artifactId>
<packaging>war</packaging>
<version>0.0.2</version>
<name>myapplication</name>
<description>My Application</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- exclude tomcat-jdbc, Spring Boot will use HikariCP automatically -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.2.2.jre8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Is there any problem with the POM.xml ?
Also below is my application.properties
server.servlet.context-path=/myapplication
I've already trid to remove the context path and rebuild it again, but it still the same.
Apparently I must append this dependency in POM.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
Also I must add this
#SpringBootApplication
public class SpringBootTomcatApplication extends SpringBootServletInitializer {
}
Now it's working thanks.

How to run Spring app on localhost without IntelliJ?

My app works when run from IntelliJ but I have issues trying to run it without help of IntelliJ.
Main class:
#SpringBootApplication
public class LoadingsApplication {
public static void main(String[] args) {
SpringApplication.run(LoadingsApplication.class, args);
}
}
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>pl.margol</groupId>
<artifactId>loadings</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>loadings</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties:
server.port=8087
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2
# Datasource
spring.datasource.url=jdbc:h2:file:~/loadings-db;
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.hibernate.ddl-auto=update
.war file was created and deployed in Tomcat Web Application Manager.
When I call localhost:8080/loadings-0.0.1-SNAPSHOT/start
i get error:
HTTP Status 404 – Not Found
Type Status Report
Message /loadings-0.0.1-SNAPSHOT/start
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Apache Tomcat/9.0.20
I was trying to change port in application.properties file but it didn't help.
Can You help to solve this problem?
I think your application didn't deployed at all.
Since you are deploying your application as war file in external container you will need to extend SpringBootServletInitializer.
Your class will look like this:
#SpringBootApplication
public class LoadingApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(HelloServerApplication.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder)
{
return builder.sources(HelloServerApplication.class);
}
}
Why are you using this dependency:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
You don't need it or mark it's scope as provided
Also you may need to exclude tomcat from spring-boot-starter-weblike this:
<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>
And Since you are using external container server.port=8087 will not work.
Try this.
#SpringBootApplication
public class LoadingsApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(LoadingsApplication.class, args);
}
}

Spring Boot controller redirect to /template jsp page NOT WORKING

I am trying to redirect from a very simple controller to a jsp page that is located in resources/templates, but I don't want to use ViewResolver java configuration, instead I want to use application properties prefix and suffix!
Controller
#GetMapping("/")
public String redirectToMainPage() {
return "main-page";
}
and these are my application properties:
spring.mvc.view.prefix = /templates/
spring.mvc.view.suffix = .jsp
the jsp location is:
/src/main/resources/templates/main-page.jsp
But i always get this error:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Nov 13 13:38:55 EET 2019
There was an unexpected error (type=Not Found, status=404).
No message available
EDIT: I just created the project from Spring initializr. Below is the important bits, the others are just autogenerated dependendecies
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>web.site</groupId>
<artifactId>cookBook</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cookBook</name>
<description>Demo project for Spring Boot</description>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Thanks for the help in advance!
Please add the dependency :
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
The embedded jasper dependency is needed to render the jsp files. Also, change your packaging to war when you are using jsp as view renderer.

Controlling the URL for a Java-web application (esp. Spring)

The book Spring in Action 5th Edition comes with some downloadable source code, which is organized by chapters. The link for the downloadable software is as follows:
https://www.manning.com/downloads/1599
Here is the POM file for the Chapter 1 sample code:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>sia</groupId>
<artifactId>taco-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <!--1-->
<name>taco-cloud</name>
<description>Taco Cloud Example</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version> <!--2-->
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>
UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>
UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency> <!--3-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>htmlunit-driver</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin> <!--4-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The URL to invoke the application (running on my local server) is as follows:
http://localhost:8080/
I would like to make changes (to the POM?) so that I can specify the chapter number with URL. Specifically I would like to invoke the application with the following URL:
http://localhost:8080/Chapter01
The #GetMapping annotation in the controller code uses the root path (i.e. "/"), which I want to retain.
Kindly suggest pointers.
For the controller to have a common base address or URL path, what you can do is add #RequestMapping("/Chapter01") annotation to your controller.
Your controller will look something like below:
#RestController
#RequestMapping("/Chapter01")
public class Controller {
#GetMapping("/")
// some get method
}
Now you use the following URL to call this method: http://localhost:8080/Chapter01/

converting spring boot jar to war

I developed a spring boot project. Now my requirement is to convert the jar package into war.
1. I converted the jar into war. After converting, I deployed the war into the tomcat server and tried to hit the web service.
#EnableAsync
#RestController
#EnableAutoConfiguration
#SpringBootApplication
#ComponentScan(basePackages="com.app.sensiple")
public class SensipleApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SensipleApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SensipleApplication.class, args);
}
#RequestMapping("/test")
String test() {
return "This is test and it's Working fine!";
}
POM.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.app</groupId>
<artifactId>snservice</artifactId>
<version>443-async</version>
<packaging>war</packaging>
<name>sensiple</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
UrL: localhost:8080/snservice/test
The above url is working fine.
My application contains different packages like Controller, Service and Repo. I have added the screenshot of my project structure.
The issue is i can't able to hit the services in controller package. Do i need to configure dispatcher Servlet for this???? . Anybody please help me out?
If you are using maven you can indicate it in pom.xml packaging
<packaging>war</packaging>
<!-- ... -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- ... -->
</dependencies>
Next you need to provide a SpringBootServletInitializer subclass and override its configure method.
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
We need 3 steps for convert JAR to WAR in Spring Boot:
Change packaging to war
Marked the embedded tomcat as provided.
Extends SpringBootServletInitializer and override the configure()
method.
I have converted jar to war in spring boot by using given below website (Explained step by step).
https://tecmentor.in/spring-boot/convert-jar-to-war-in-spring-boot/

Categories