当 url 以 .xhtml

问题描述

我在 Spring Boot 中有以下端点:

 @RestController
    @RequestMapping(value = "/")
    class HelloXhtml {

    @RequestMapping(value = {"/{app}/form.xhtml"},method = {RequestMethod.GET},produces = {APPLICATION_XHTML_XML_VALUE})
    public ResponseEntity<String> getFormDefinitionXml(
            @PathVariable(name = "app") String app) {
            return new ResponseEntity<>("<fruit>apple</fruit>",HttpStatus.OK);

    }
}

如果我使用嵌入式 Tomcat 运行它,一切正常,但在 Wildfly 20.0.0-Final 上,此特定端点会抛出 404。

当@RequestMapping 值以“.xhtml”结尾时,端点失败并显示 404,如下所示:

@RequestMapping(value = {"/{app}/form.xhtml"}

我尝试通过将 @RequestMapping 字符串更改为:

@RequestMapping(value = {"/{app}/{whatever}"}

它产生了相同的结果:当“whatever”参数是任何字符串时,它按预期工作,并在我传入以 xhtml 结尾的文件名(例如:apple.xhtml)作为参数时返回 404 错误。

是什么让 Wildfly 丢弃任何以 xhtml 结尾的 url 并返回 404,我该如何关闭这种行为?

编辑:

我又参加了一轮。我创建了一个示例 spring 应用程序来产生行为:

Java 代码:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

import java.time.ZonedDateTime;

import static org.springframework.http.MediaType.APPLICATION_XHTML_XML_VALUE;
import static org.springframework.http.MediaType.APPLICATION_XML_VALUE;

@SpringBootApplication
public class DemoApplication  extends SpringBootServletInitializer {

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

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

}

@RestController
@RequestMapping(value = "/persistence/crud")
class HelloXhtml {


    @RequestMapping(value = {"/{app}/{form}/form/{formXml}"},produces = {APPLICATION_XHTML_XML_VALUE})
    public ResponseEntity<String> getFormDefinitionXml(
            @PathVariable(name = "app") String app) {
            return new ResponseEntity<>("<fruit>pear</fruit>",HttpStatus.OK);

    }
}

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 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.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
                <plugin>
                <groupId>org.wildfly.plugins</groupId>
                <artifactId>wildfly-maven-plugin</artifactId>
                <version>2.0.0.Final</version>
            </plugin>
            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
            <failOnMissingWebXml>false</failOnMissingWebXml>
                <archive>                   
                    <manifestEntries>
                        <Dependencies>jdk.unsupported</Dependencies>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
        </plugins>
    </build>

</project>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)