我迁移到 spring boot 3 和 java 21。由于我要进行补丁休息调用,因此我需要依赖项 org.apache.httpcomponents.client5:httpclient5:5.2.2。
但有了这种依赖性,如果将 org.springframework.web.client.RestTemplate 与 RequestEntity.get 一起使用,我会得到“java.lang.IllegalArgumentException:无效代理”。
在相同的设置下,补丁调用工作得很好。
有谁知道为什么会发生这种情况?我没有代理。我必须定义默认代理吗?
不幸的是,对于此链接:https://github.com/apache/httpcomponents-client/tree/5.1.x/httpclient5/src/test/java/org/apache/hc/client5/http/examples/ClientConfiguration.java 我得到 404。:-(
我尝试仅使用 apache 依赖项而不使用 Resttemplate 并得到相同的异常。
HttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(uri);
httpGet.setHeader(HttpHeaders.AUTHORIZATION, token);
httpGet.setHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
Run Code Online (Sandbox Code Playgroud) apache-httpclient-5.x spring-resttemplate java-21 spring-boot-3
我的 Spring Boot 应用程序面临一个问题,其中我有一个仅处理 POST 请求的控制器。application.yml
我已经在我的as中设置了上下文路径server.servlet.context-path: /context/path
。目标是处理/context/path
和/context/path/
URL 的 POST 请求。
我的控制器看起来像这样:
@RestController
@RequestMapping("")
public class MyController {
@PostMapping({ "", "/" })
public ResponseEntity<String> handlePostRequest() {
// Handling POST request logic
return ResponseEntity.ok("POST request handled");
}
}
Run Code Online (Sandbox Code Playgroud)
当我向 发送 POST 请求时/context/path
,它会被重定向为 302 状态代码,并且请求方法更改为 GET,并且会被重定向到/context/path/
.
我尝试过@RequestMapping
和的不同组合PostMapping
。什么都没起作用。
我找到了一些推荐的解决方案来创建 WebConfiguration 并覆盖 configurePathMatch 方法。setUseTrailingSlashMatch
但类似或 的方法setMatchOptionalTrailingSeparator
已被弃用。
尽管做出了这些尝试,问题仍然存在。如何配置我的应用程序来处理带或不带尾部斜杠的请求?任何有关解决此问题的见解或建议将不胜感激。
当使用我的 Eclipse 的 Tomcat9 服务器(v9.0.62)时,我的“应用程序”运行正常。然而,一旦部署到我的生产服务器上(v9.0.43),就会返回 404。Tomcat 没有记录任何堆栈跟踪。
两台服务器都使用 OpenJDK17 作为 JDK。使用 Spring < 6 版本时不会出现此问题。
Eclipse 控制台输出:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot …
Run Code Online (Sandbox Code Playgroud)