第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

SpringBoot 開(kāi)發(fā)一個(gè)可以在 Web 容器中運(yùn)行的 War 程序

標(biāo)簽:
Spring

前言

前面的文章的 web 程序的样例都是一个个独立运行的 jar 包的形式。这种方式使用起来很方便,但是也有时候有需求要将 SpringBoot 开发的 web 程序放到一个 web 容器例如 Tomcat 中去运行。这个时候就需要将 SpringBoot 程序给打包成一个标准的 war 包文件才行。怎么做呢,其实也很简单,下面我们来演示一下。

创建标准 SpringBoot web 项目

根据前面的文章,我们在 IDEA 里面创建一个标准的 SpringBoot 程序,他的 Maven 配置文件 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.yanggaochao.demo</groupId>
    <artifactId>war_demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>war_demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-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>

然后我们编写一个 Rest 服务接口如下

package com.yanggaochao.demo.war;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;/**
 * Hello 服务
 *
 * @author : 杨高超
 * @since : 2018-11-20
 */@RestController@RequestMapping("/api/v1/hello")
public class HelloService {
    @RequestMapping(value = "/echo/{name}", method = RequestMethod.GET)
    public String sayHello(@PathVariable String name) {        return "Hello," + name;

    }
}

然后,我们配置 application.propertis 内容如下

server.servlet.context-path=/spring_boot_war
server.port=3333

运行主程序。那么在浏览器中输入地址 http://localhost:3333/spring_boot_war/api/v1/hello/echo/yanggch 就会在页面上显示 Hello,yanggch 的文本。

这就是一个标准的 SpringBoot Web 程序了。下面我们要把他改造成为一个可以打包为标准的 war 包文件,放到 Java Web 容器中运行的程序。

改造为 War 包程序

首先,我们要修改我们的启动类,让他继承一个 org.springframework.boot.web.servlet.support.SpringBootServletInitializer 类并且改写 config 方法。修改后的启动类变成

package com.yanggaochao.demo.war;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;/**
 * War 样例
 *
 * @author : 杨高超
 * @since : 2018-11-20
 */@SpringBootApplicationpublic class WarApplication extends SpringBootServletInitializer {    public static void main(String[] args) {
        SpringApplication.run(WarApplication.class, args);
    }    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {        return builder.sources(WarApplication.class);
    }
}

然后修改我们的 Maven 文件如下

<?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.yanggaochao.demo</groupId>
    <artifactId>war</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>war</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>spring_boot_war</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build></project>




作者:高超杨
链接:https://www.jianshu.com/p/b190d8b24010


點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消