gpt4 book ai didi

spring-boot - Resttemplate 和补丁,无效

转载 作者:行者123 更新时间:2023-12-04 23:16:15 26 4
gpt4 key购买 nike

我使用 Spring 1.4.3

我尝试调用网络服务

  @PatchMapping(value = "/members/{memberId}/card")
public ResponseEntity updateMemberCardId(@PathVariable("memberId") Long memberId, @RequestBody String cardId) throws ResourceNotFoundException {
memberService.updateMemberCardId(cardId, memberId);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

在我的申请中,
@Component
@Configuration
public class ClientRestConfig {

@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder, @Value("${main.server.url}") String mainServerUrl, @Value("${commerce.username}") String commerceUsername, @Value("${commerce.password}") String commercePassword, @Value("${connection.timeout}") int timeout) {
return builder.setConnectTimeout(timeout).setReadTimeout(timeout).basicAuthorization(commerceUsername, commercePassword).rootUri(mainServerUrl).build();
}

}

在另一种方法中我做
String cardId = "123456789";

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>(cardId, headers);

ResponseEntity responseEntity = restTemplate.patchForObject("/rest/members/1/card", entity, ResponseEntity.class);

我收到这个错误

java.net.ProtocolException: Invalid HTTP method: PATCH at java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:440) ~[na:1.8.0_111] at sun.net.www.protocol.http.HttpURLConnection.setRequestMethod(HttpURLConnection.java:552) ~[na:1.8.0_111]

最佳答案

基于 ritesh.garg 的回答:

添加以下内容 dependency到您的类路径:

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>

然后,创建您的 RestTemplate像这样:
    RestTemplate template = new RestTemplate(new HttpComponentsClientHttpRequestFactory());

关于spring-boot - Resttemplate 和补丁,无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41368762/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com