Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resttemplate and patch, invalid

I use spring 1.4.3

I try to call a web service

  @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);
  }

In my application,

@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();
    }

}

In another method I do

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);

I get this error

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]

like image 677
robert trudel Avatar asked Dec 28 '16 19:12

robert trudel


People also ask

Why RestTemplate is deprecated?

RestTemplate provides a synchronous way of consuming Rest services, which means it will block the thread until it receives a response. RestTemplate is deprecated since Spring 5 which means it's not really that future proof. First, we create a Spring Boot project with the spring-boot-starter-web dependency.

How do I call a patch method in Java?

To send a PATCH request to the server, you need to use the HTTP PATCH method and include the request data in the body of the HTTP message. The Content-Type request header must indicate the data type in the body. In this PATCH request example, we send JSON to the ReqBin echo endpoint to update the data on the server.

Does RestTemplate support all HTTP methods?

Spring RestTemplate class is part of spring-web , introduced in Spring 3. We can use RestTemplate to test HTTP based restful web services, it doesn't support HTTPS protocol. RestTemplate class provides overloaded methods for different HTTP methods, such as GET, POST, PUT, DELETE etc.

Is RestTemplate Exchange blocking?

RestTemplate uses Java Servlet API and is therefore synchronous and blocking.


1 Answers

Building on ritesh.garg's answer:

Add the following dependency to your classpath:

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

Then, create your RestTemplate like this:

    RestTemplate template = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
like image 93
cstroe Avatar answered Jan 04 '23 21:01

cstroe