Skip to content

Instantly share code, notes, and snippets.

@condran
Last active May 30, 2022 19:18
Show Gist options
  • Save condran/36819504b5811c01f3bc2f179a85b065 to your computer and use it in GitHub Desktop.
Save condran/36819504b5811c01f3bc2f179a85b065 to your computer and use it in GitHub Desktop.
Spring Cloud Netflix with PATCH (client side)

Spring Cloud Netflix with PATCH (client side)

I have been researching Spring Boot and Spring Cloud, there are lots of goodies here but as with all technology there are gotchas.

Feign Client does not support PATCH out of the box! ಠ_ಠ ... ಠ~ಠ ... ⊙︿⊙

You might see an error like this if you try to use RequestMethod.PATCH in your @FeignClient interface: java.net.ProtocolException: Invalid HTTP method: PATCH

I am using the current versions:

Spring Project Version
Spring Cloud Brixton.RELEASE
Spring Boot 1.3.5.RELEASE

You need to include this library in your pom.xml, and then Feign will pick it up, so that's not too bad but the info is hard to find in various StackOverflow posts / docs / Google links

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
		</dependency>
		<dependency>
			<!-- Required to use PATCH -->
			<groupId>com.netflix.feign</groupId>
			<artifactId>feign-httpclient</artifactId>
		</dependency>

Your spring boot application class will look something like this (if you want to use HAL from Feign)

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
public class SpringBootApplication {
	// main, etc
}

Finally, make sure your FeignClient interface is properly defined

// Use spring application service id, or URL, whatever you need to find the REST service
@FeignClient("book-data-rest-discovery-service-id")
public interface SpringDataRESTClient {

    @RequestMapping(method = RequestMethod.GET, value = "/books", consumes= MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    PagedResources<Book> getBookData(@RequestParam("page") int page);

    @RequestMapping(method = RequestMethod.GET, value = "/books/{id}", consumes= MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    Resource<Book> getBook(@PathVariable("id") String id);

    @RequestMapping(method = RequestMethod.POST, value = "/books", consumes= MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    void save(Book newBook);

    // You can also use RequestMethod.PUT, which is better supported than PATCH by default (no need to include extra 'feign-httpclient' in Maven pom)
    @RequestMapping(method = RequestMethod.PATCH, value = "/books/{bookId}", consumes= MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    Book update(@PathVariable("bookId") UUID bookId, Book book);

}
@msuganthan
Copy link

PUT call is not working after adding com.netflix.feign:feign-httpclient:8.18.0

@edmarcoslins
Copy link

I solved with this dependency
<dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-httpclient</artifactId> <version>9.5.0</version> </dependency>

@biki636
Copy link

biki636 commented Feb 6, 2019

This PATCH implementation for feign client is not working the way it should be.
According to REST specs, PUT is a "complete replacement" and PATCH is a "partial replacement", however here PATCH is working exactly the same as PUT.

If the PATCH is going to work like PUT then what is the need of this PATCH method in the first place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment