我有一个springboot(v2.3.5.release)restapi,似乎无法解决路径变量的编码问题。我尝试了网上找到的每一个解决方案,但都无济于事。以下是主要课程:
@SpringBootApplication
public class RestApplication {
public static void main(String[] args) {
SpringApplication.run(RestApplication.class, args);
}
}
restcontroller(将请求发送到该url时从不执行该方法):
@RestController
@RequestMapping("/mvn/packages")
public class ModuleApi {
@Autowired
ModuleApiService service;
@GetMapping(value = "/{pkg}/{pkg_ver}/modules/{namespace}/metadata", produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<String> getModuleMetadata(@PathVariable("pkg") String package_name,
@PathVariable("pkg_ver") String package_version,
@PathVariable("namespace") String module_namespace) {
return service.getModuleMetadata(package_name, package_version, module_namespace);
}
}
配置类:
@Configuration
public class CorsConfiguration
{
@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setForceEncoding(true);
characterEncodingFilter.setEncoding("UTF-8");
registrationBean.setFilter(characterEncodingFilter);
return registrationBean;
}
}
这个 application.properties
:
logging.level.org.springframework.web=DEBUG
server.port=8080
server.tomcat.uri-encoding=UTF-8
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.enabled=true
server.servlet.encoding.force=true
我甚至把编码添加到 spring-boot-maven-plugin
在 pom.xml
:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.6.RELEASE</version>
<configuration>
<mainClass>RestApplication</mainClass>
<jvmArguments>-Dfile.encoding=UTF8</jvmArguments>
</configuration>
</plugin>
但结果还是一样。向发送请求时 http://localhost:8080/mvn/packages/junit:junit/4.12/%2Fjunit.framework%2FAssert
即 mvn/packages/{pkg}/{pkg_ver}/modules/{namespace}/metadata
以及 namespace
被编码,它返回 HTTP 400 - BAD REQUEST
.
然而,当我尝试 http://localhost:8080/mvn/packages/junit:junit/4.12/foo
(不需要编码/解码),它可以工作。
我也试过用 ALLOW_ENCODED_SLASH
财产。主要类别:
@SpringBootApplication
public class RestApplication {
public static void main(String[] args) {
System.setProperty("org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH", "true");
SpringApplication.run(RestApplication.class, args);
}
}
但在这种情况下,它无法将请求解析为该Map并返回 404 - NOT FOUND
:
2020-12-10 17:03:27.044 DEBUG 105120 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : GET "/mvn/packages/junit:junit/4.12/%2Fjunit.framework%2FAssert", parameters={}
2020-12-10 17:03:27.053 DEBUG 105120 --- [nio-8080-exec-1] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]
2020-12-10 17:03:27.056 DEBUG 105120 --- [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
2020-12-10 17:03:27.056 DEBUG 105120 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
2020-12-10 17:03:27.060 DEBUG 105120 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for GET "/error", parameters={}
2020-12-10 17:03:27.062 DEBUG 105120 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2020-12-10 17:03:27.083 DEBUG 105120 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
2020-12-10 17:03:27.087 DEBUG 105120 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404
有人能帮我吗?
暂无答案!
目前还没有任何答案,快来回答吧!