欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot 项目打成war包部署到tomcat下

程序员文章站 2024-02-02 23:10:22
...

一.打包方式的概述

1.1、ide启动

1.2  jar包方式

  maven插件:
                <build>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                    </plugin>
                </plugins>
                </build>
                如果没有加,则执行jar包 ,报错如下
                    java -jar spring-boot-demo-0.0.1-SNAPSHOT.jar
                    no main manifest attribute, in spring-boot-demo-0.0.1-SNAPSHOT.jar
                如果有安装maven 用 mvn spring-boot:run

1.3 war包方式启动

1)在pom.xml中将打包形式 jar 修改为war  <packaging>war</packaging>

        构建项目名称 <finalName>xdclass_springboot</finalName>

        2)tocmat下载 https://tomcat.apache.org/download-90.cgi
        
        3)修改启动类
            public class XdclassApplication extends SpringBootServletInitializer {

                @Override
                protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
                    return application.sources(XdclassApplication.class);
                }

                public static void main(String[] args) throws Exception {
                    SpringApplication.run(XdclassApplication.class, args);
                }

            }

        4)打包项目,启动tomcat

     二.新建项目 springboot的war包部署到tomcat下

2.1 配置pom文件

注意springboot-starter-boot-tomcat这个依赖注释掉

<?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.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ljf.spt.war.deployment</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>12-spt-war-deployment</name>
    <description>Demo project for Spring Boot</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-starter-tomcat</artifactId>
            <scope>provided</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>
    </dependencies>

    <build>
        <!-- 打包的项目名称 -->
        <finalName>12-spt-war-deployment</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>

    </build>

</project>

2.2  controller

package com.ljf.spt.war.deployment.demo.controller;

import com.ljf.spt.war.deployment.demo.model.JsonData;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: UserController
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2020/09/08 08:31:37 
 * @Version: V1.0
 **/
@RestController
public class UserController {
    /**
     * 功能描述:微信支付回调接口
     * @param msg 支付信息
     * @return
     */
    @GetMapping("order")
    public Object order(String msg){

        return JsonData.buildSuccess("beijing","ok");
    }
}

2.3 domain

package com.ljf.spt.war.deployment.demo.model;

import java.io.Serializable;

/**
 * @ClassName: JsonData
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2020/09/08 09:04:49 
 * @Version: V1.0
 **/
public class JsonData implements Serializable {
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    private Integer code; // 状态码 0 表示成功,1表示处理中,-1表示失败
    private Object data; // 数据
    private String msg;// 描述

    public JsonData() {
    }

    public JsonData(Integer code, Object data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    // 成功,传入数据
    public static JsonData buildSuccess(String name,String msg) {
        return new JsonData(0, name, msg);
    }

    // 成功,传入数据
    public static JsonData buildSuccess(Object data) {
        return new JsonData(0, data, null);
    }

    // 失败,传入描述信息
    public static JsonData buildError(String msg) {
        return new JsonData(-1, null, msg);
    }

    // 失败,传入描述信息,状态码
    public static JsonData buildError(String msg, Integer code) {
        return new JsonData(code, null, msg);
    }

    // 成功,传入数据,及描述信息
    public static JsonData buildSuccess(Object data, String msg) {
        return new JsonData(0, data, msg);
    }

    // 成功,传入数据,及状态码
    public static JsonData buildSuccess(Object data, int code) {
        return new JsonData(code, data, null);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public String toString() {
        return "JsonData [code=" + code + ", data=" + data + ", msg=" + msg
                + "]";
    }
}

2.4 users

package com.ljf.spt.war.deployment.demo.model;

import java.util.Date;

/**
 * @ClassName: User
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2020/09/08 08:51:33 
 * @Version: V1.0
 **/
public class User {
    private int age;

    private String pwd;

    private String phone;

    private Date createTime;



    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public User() {
        super();
    }

    public User(int age, String pwd, String phone, Date createTime) {
        super();
        this.age = age;
        this.pwd = pwd;
        this.phone = phone;
        this.createTime = createTime;
    }

}

2.5 utils

package com.ljf.spt.war.deployment.demo.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.util.StringUtils;

import java.io.IOException;

/**
 * @ClassName: JsonUtils
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2020/09/08 08:33:25 
 * @Version: V1.0
 **/
public class JsonUtils {
    private static ObjectMapper objectMapper = new ObjectMapper();

    //对象转字符串
    public static <T> String obj2String(T obj){
        if (obj == null){
            return null;
        }
        try {
            return obj instanceof String ? (String) obj : objectMapper.writeValueAsString(obj);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //字符串转对象
    public static <T> T string2Obj(String str,Class<T> clazz){
        if (StringUtils.isEmpty(str) || clazz == null){
            return null;
        }
        try {
            return clazz.equals(String.class)? (T) str :objectMapper.readValue(str,clazz);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

2.6 启动类

package com.ljf.spt.war.deployment.demo;

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 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);
        System.out.println("启动成功!!!");
    }

}

2.7 打war包

springboot 项目打成war包部署到tomcat下

2.8 部署到tomcat下

springboot 项目打成war包部署到tomcat下

启动startup.bat脚本

springboot 项目打成war包部署到tomcat下

2.9 页面访问

springboot 项目打成war包部署到tomcat下

相关标签: springboot