SpringBoot frequently asked questions

Q. Can I deploy springboot application to external tomcat server ? If yes then how ?

 

A. Yes, Generally in the real-time projects we wont use inbuilt servers provided by the frameworks because of many reasons like security, maintenance and control.

Just do below changes to your spring boot application which you want to deploy it on to external tomcat server.

 

1.Add below dependency and packaging tag in pom.xml file

<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.consyosoft</groupId>
    <artifactId>SpringBootAppToExterServ</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    
    <!-- packaging to WAR -->
    <packaging>war</packaging>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
    </parent>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- Instruct spring boot not to use the inbuilt Tomcat server -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <properties>
        <java.version>1.8</java.version>
    </properties>
</project>

 

 

 

2.Extend main Class with SpringBootServletInitializer

package com.java4s.app;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
 
@SpringBootApplication
public class SpringBootPro1 extends SpringBootServletInitializer {
 
 
 @Override
 protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  return application.sources(SpringBootPro1.class);
 }
 
 public static void main(String[] args) {
  SpringApplication.run(SpringBootPro1.class, args);
 }
}


3. Generate a WAR file

Right click on pom.xml -> Run as -> Maven install, this will generate a WAR file in your target folder.  Just copy that WAR file into your tomcat’s webapps folder and start the server ( or ) you can deploy and test from your IDE (eclipse/sts) by importing that WAR into your work space and run that in the external tomcat 

 

 

Q. Difference between snapshot and release version

A.Snapshots are changable, releases are unchangeable.

 

 

 

Q. How to create spring boot web app folder structure

A. We need to create WEB-INF folder manually in spring boot application. Follow below steps to create WEB-INF folder

1.      Select main folder as highlighted below.



2.      Right click and create a folder with name webapp.

3.      Now right click on webapp folder and create WEB-INF folder.

4.      Now similarly create a view folder (or any other name you like) inside WEB-INF folder to keep all the JSPs.

5.      Below should be the folder structure after doing above steps.






 

Q. Difference between jar and war in spring boot

A. JAR files allow us to package multiple files in order to use it as a library, plugin, or any kind of application. On the other hand, WAR files are used only for web applications.



Q. What is csrf method of httpsecurity which we use to provide security to app

A. Cross-Site Request Forgery (CSRF) is an attack that forces authenticated users to submit a request to a Web application against which they are currently authenticated. CSRF attacks exploit the trust a Web application has in an authenticated user.

 

 


Comments

Popular posts from this blog

Advanced Java Session 1 (Concept of Web Application Basics And Advantages of Spring)