Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does SpringBoot package and deploy jar to Tomcat

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how SpringBoot packages and deploys jar to Tomcat". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Detailed steps

First, make some changes in the pom.xml file:

Before you hit the war package, you need to modify the packaging method, but not this time, because the default is that the jar package specifies the name of the jar package and manually specifies the resources folder to compile and package the path to add the dependency of SpringBoot embedded Tomcat parsing jsp (just for this instance).

4.0.0 org.springframework.boot spring-boot-starter-parent 2.4.5 com.songzihao.springboot 023-springboot-jar 1.0.0 11 org.apache.tomcat.embed tomcat-embed-jasper org.springframework.boot Spring-boot-starter-web SpringBootJar src/main/webapp META-INF/resources *. * src/main/resources * * /. * org.springframework.boot spring-boot-maven-plugin 1.4.2.RELEASE

Then write a control layer, UserController

Package com.songzihao.springboot.controller; import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap;import java.util.Map; / * * * / @ Controllerpublic class UserController {@ RequestMapping (value = "/ user/detail") public @ ResponseBody Object userDetail () {Map map=new HashMap (); map.put ("id", 1001) Map.put ("username", "Zhang Qiling"); return map;} @ RequestMapping (value = "/ user/page/detail") public String userPageDetail (Model model) {model.addAttribute ("id", 1001); model.addAttribute ("username", "little brother"); return "userDetail";}}

In the core configuration file, configure the port number, context root, and view parser with embedded Tomcat.

Server.port=9090server.servlet.context-path=/ spring.mvc.view.prefix=/spring.mvc.view.suffix=.jsp

Then write a corresponding jsp page in the control layer and use it as a simulation test.

$user number: ${id} user name: ${username}

Finally, there is the entry class for the SpringBoot project.

Package com.songzihao.springboot; import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication; @ SpringBootApplicationpublic class Application {public static void main (String [] args) {SpringApplication.run (Application.class, args);}}

1.1 start the test in IDEA

1.2 after the project has been packaged and deployed, start the test again

After typing the war package before, we need to put the generated .war file in the Tomcat's webapps directory.

This time we typed the jar package and generated a .jar file, which we can put in any directory.

Copy the SpringBootJar.jar in the specified directory, and in the current directory, type cmd to execute the command line window.

Then enter the command: java-jar SpringBootJar.jar, start!

1.3 changes in the port number and context root of the Tomcat after the jar package of the SpringBoot project

Before the war package, after the project is deployed in Tomcat, the Tomcat port number and context root in our core configuration file are invalid, subject to the local Tomcat.

This time it is the jar package, because it is not deployed to the local Tomcat, but rather, we are still using the embedded Tomcat provided by the SpringBoot framework, so what works at this time should be the port number and context root declared in the core configuration file application.properties!

That's all for "how SpringBoot packages and deploys jar to Tomcat". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report