1

我有一个带有以下代码的spring boot项目:

import org.springframework.web.context.request.NativeWebRequest;
import javax.servlet.http.HttpServletResponse

foo(NativeWebRequest req){
    HttpServletResponse res = req.getNativeResponse(HttpServletResponse.class);
    res.setCharacterEncoding("UTF-8");
}

在我添加速度依赖项之前一切正常:

compile group: 'org.apache.velocity', name: 'velocity', version: '1.7'
compile group: 'org.apache.velocity', name: 'velocity-tools', version: '2.0'

现在我得到:
在 Eclipse 中:The method setCharacterEncoding(String) is undefined for the type HttpServletResponse
并且在gradle bootJar

.../SomeFile.java:12: error: cannot find symbol
            res.setCharacterEncoding("UTF-8");
               ^
  symbol:   method setCharacterEncoding(String)
  location: variable res of type HttpServletResponse

所以我认为导入 HttpServletResponse 的库版本在某种程度上受到这些依赖项的影响。

我的春季启动插件是:id 'org.springframework.boot' version '2.3.2.RELEASE'

同一项目中的所有依赖项是:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'

    // https://mvnrepository.com/artifact/org.apache.velocity/velocity
    compile group: 'org.apache.velocity', name: 'velocity', version: '1.7'

    // https://mvnrepository.com/artifact/org.apache.velocity/velocity-tools
    compile group: 'org.apache.velocity', name: 'velocity-tools', version: '2.0'
}

gradle dependencies我可以看到速度集javax.servlet:servlet-api:2.3但没有速度依赖javax.servlet项根本不会出现在 gradle 依赖项中,所以我不知道之前的版本是什么。

通过注释掉,我可以确定implementation 'org.springframework.boot:spring-boot-starter-web'如果速度不是依赖项,则提供 javax.servlet。(根据gradle dependencies版本 2.3.2 incompileClasspath(n)in implementation

我也试过gradle dependencyInsight --configuration compile --dependency javax.servlet:servlet-api没有速度,但得到了No dependencies matching given input were found in configuration 'compile'.

我认为这些问题非常接近,但我仍然不确定如何在 gradle 中进行:setCharacterEncoding questions
Apache
Velocity + Servlet 3.0

4

1 回答 1

0

替换
compile group: 'org.apache.velocity', name: 'velocity-tools', version: '2.0'

compile('org.apache.velocity:velocity-tools:2.0') {
    exclude group: 'javax.servlet', module: 'servlet-api'
}

为我工作。

于 2020-08-10T13:58:39.883 回答