我用弹簧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:无效的HTTP方法:PATCH位于
java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:440)
〜[na:1.8.0_111]在
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/

10-13 05:08