我是使用RestTemplate 类发送Http请求的,
public ResponseEntity<?> updateUser(User user, Long userId) {
RestTemplate restTemplate = new RestTemplate();
String url = "http://localhost:8081/api/users/user/1";
User usr = restTemplate.patchForObject(url, user, User.class, userId);
User usr = this.restTemplate.patchForObject(url, user, User.class);
return new ResponseEntity<User>(usr, HttpStatus.OK);
}
报错 I/O error on PATCH request for "http://localhost:8081/api/users/user/1". Invalid HTTP method: PATCH.
然后把 代码修改一下:
public ResponseEntity<?> updateUser(User user, Long userId) {
RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
String url = "http://localhost:8081/api/users/user/1";
User usr = restTemplate.patchForObject(url, user, User.class, userId);
User usr = this.restTemplate.patchForObject(url, user, User.class);
return new ResponseEntity<User>(usr, HttpStatus.OK);
}
就好了。
注意:
需要添加依赖:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
我用的是SpringBoot 所以没有加version.