spring 无法从上下文/应用程序将请求字符编码设置为UTF-8,因为已读取请求参数

7lrncoxx  于 2023-06-28  发布在  Spring
关注(0)|答案(1)|浏览(139)

我已经设置了环境:
org.springframework. Boot 3.1.0 openJDK 17.0.2 Payara Server 6.2023.5
可以使用以下pom.xml成功启动Spring应用程序

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.1.0</version>
    <relativePath/>
</parent>

<properties>
    <java.version>17</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>jakarta.platform</groupId>
        <artifactId>jakarta.jakartaee-api</artifactId>
        <version>9.1.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

<build>
    <plugins>   
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.2.3</version>
            <configuration>
              <!-- War plugin configuration goes here -->
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>17</source>
                <target>17</target>
            </configuration>
        </plugin>
    </plugins>
</build>

然后我设置RequestMapping如下:

@Controller
    public class UiController {                
    @RequestMapping(value = "/", method = RequestMethod.GET)
    @ResponseBody
    public String showDefault() { 
        return "<p>Hello World</p>";
    }
      
}

可以成功显示“Hello World”,但会记录以下警告:
[2023-05-30T15:06:38.410+0000] [Payara 6.2023.5] [警告] [AS-WEB-CORE-00059] [jakarta.enterprise.web.core] [tid:_ThreadID=79 _ThreadName=http-thread-pool::http-listener-2(3)] [timeMillis:1685459198410] [levelValue:900] 无法从上下文/应用程序将请求字符编码设置为UTF-8,因为已读取请求参数,或已调用ServletRequest.getReader()
它一直显示一条消息,警告编码不能更改,但我没有尝试更改函数中的编码,应用程序也没有调用getReader。我试着测试“GetMapping”,但类似的警告是get。我不知道怎么修。

mrfwxfqh

mrfwxfqh1#

看起来Payara和Spring在请求字符编码上不一致。Spring可能默认为iso-8859-1,并在Payara获得机会之前读取请求参数。
您可以尝试在RequestMapping中指定UTF-8作为请求字符编码:

@RequestMapping(value = "/", method = RequestMethod.GET, consumes = "plain/text;charset=UTF-8")

或在www.example.com文件中全局设置application.properties:

server.servlet.encoding.charset=UTF-8
server.servlet.encoding.force=true

相关问题