亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringBoot-定制Web容器Tomcat,請求頭增加traceid參數

發布時間:2020-07-15 11:25:04 來源:網絡 閱讀:1536 作者:shayang88 欄目:編程語言
通過特定Web容器工廠TomcatServletWebServerFactory定制

1、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>com.jane</groupId>
    <artifactId>tralink</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>tralink</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
        </dependency>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

</project>

2、Controller文件Test

package com.jane.tralink.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/test")
public class Test {

    @PostMapping("/hello")
    public String hello(HttpServletRequest request) {
        System.out.println( request.getHeader("traceid"));
        return "hello";
    }
}

3、自定義Web容易定制器

package com.jane.tralink.controller;

import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;

@Component
public class MyTomcatCustomize implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {

    @Override
    public void customize(TomcatServletWebServerFactory factory) {
        //TraceValve 是實際的增加Header的類
        factory.addEngineValves(new TraceValve());
    }
}

4、TraceValve

package com.jane.tralink.controller;

import org.apache.catalina.Valve;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.valves.ValveBase;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.UUID;

public class TraceValve extends ValveBase {

    @Override
    public void invoke(Request request, Response response) throws IOException, ServletException {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        request.getCoyoteRequest().getMimeHeaders().addValue("traceid").setString(uuid);

        Valve next = getNext();
        if(null == next) return;
        next.invoke(request, response);
    }
}

5、運行結果:http://127.0.0.1:8081/test/hello
SpringBoot-定制Web容器Tomcat,請求頭增加traceid參數

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

临洮县| 随州市| 沽源县| 定陶县| 汤原县| 卢龙县| 织金县| 厦门市| 壤塘县| 安龙县| 武川县| 珠海市| 阿合奇县| 莲花县| 威信县| 深泽县| 仙居县| 肥城市| 岑巩县| 水城县| 凌云县| 嘉兴市| 德安县| 崇明县| 阜新| 吴旗县| 永胜县| 桐柏县| 淮滨县| 右玉县| 桓仁| 浠水县| 连山| 天柱县| 磐安县| 沽源县| 昌邑市| 禹城市| 蒲江县| 阿合奇县| 当阳市|