Browse Source

Add composed Spring annotations support (#1090)

* Add composed Spring annotations support
pull/1093/head
Daniil Kudryavtsev 5 years ago committed by Marvin Froeder
parent
commit
2dbe24a40d
  1. 15
      spring4/README.md
  2. 47
      spring4/src/main/java/feign/spring/SpringContract.java
  3. 11
      spring4/src/test/java/feign/spring/SpringContractTest.java

15
spring4/README.md

@ -18,6 +18,21 @@ The ```consume``` adds the first value as the `Content-Type` header. @@ -18,6 +18,21 @@ The ```consume``` adds the first value as the `Content-Type` header.
#### `@RequestMapping`
Appends the value to `Target.url()`. Can have tokens corresponding to `@PathVariable` annotations.
The method sets the request method.
#### `@GetMapping`
Appends the value to `Target.url()`. Can have tokens corresponding to `@PathVariable` annotations.
Sets the `GET` request method.
#### `@PostMapping`
Appends the value to `Target.url()`. Can have tokens corresponding to `@PathVariable` annotations.
Sets the `POST` request method.
#### `@PutMapping`
Appends the value to `Target.url()`. Can have tokens corresponding to `@PathVariable` annotations.
Sets the `PUT` request method.
#### `@DeleteMapping`
Appends the value to `Target.url()`. Can have tokens corresponding to `@PathVariable` annotations.
Sets the `DELETE` request method.
#### `@PatchMapping`
Appends the value to `Target.url()`. Can have tokens corresponding to `@PathVariable` annotations.
Sets the `PATCH` request method.
### Parameter Annotations
#### `@PathVariable`
Links the value of the corresponding parameter to a template variable declared in the path.

47
spring4/src/main/java/feign/spring/SpringContract.java

@ -13,19 +13,12 @@ @@ -13,19 +13,12 @@
*/
package feign.spring;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import feign.Contract.BaseContract;
import org.springframework.web.bind.annotation.*;
import feign.DeclarativeContract;
import feign.MethodMetadata;
import feign.Request;
public class SpringContract extends DeclarativeContract {
@ -51,6 +44,42 @@ public class SpringContract extends DeclarativeContract { @@ -51,6 +44,42 @@ public class SpringContract extends DeclarativeContract {
data.template().method(requestMapping.method()[0].name());
});
registerMethodAnnotation(GetMapping.class, (mapping, data) -> {
appendMappings(data, mapping.value());
data.template().method(Request.HttpMethod.GET);
handleProducesAnnotation(data, mapping.produces());
handleConsumesAnnotation(data, mapping.consumes());
});
registerMethodAnnotation(PostMapping.class, (mapping, data) -> {
appendMappings(data, mapping.value());
data.template().method(Request.HttpMethod.POST);
handleProducesAnnotation(data, mapping.produces());
handleConsumesAnnotation(data, mapping.consumes());
});
registerMethodAnnotation(PutMapping.class, (mapping, data) -> {
appendMappings(data, mapping.value());
data.template().method(Request.HttpMethod.PUT);
handleProducesAnnotation(data, mapping.produces());
handleConsumesAnnotation(data, mapping.consumes());
});
registerMethodAnnotation(DeleteMapping.class, (mapping, data) -> {
appendMappings(data, mapping.value());
data.template().method(Request.HttpMethod.DELETE);
handleProducesAnnotation(data, mapping.produces());
handleConsumesAnnotation(data, mapping.consumes());
});
registerMethodAnnotation(PatchMapping.class, (mapping, data) -> {
appendMappings(data, mapping.value());
data.template().method(Request.HttpMethod.PATCH);
handleProducesAnnotation(data, mapping.produces());
handleConsumesAnnotation(data, mapping.consumes());
});
registerMethodAnnotation(ResponseBody.class, (body, data) -> {
handleConsumesAnnotation(data, "application/json");
});

11
spring4/src/test/java/feign/spring/SpringContractTest.java

@ -47,6 +47,7 @@ public class SpringContractTest { @@ -47,6 +47,7 @@ public class SpringContractTest {
public void setup() throws IOException {
mockClient = new MockClient()
.noContent(HttpMethod.GET, "/health")
.noContent(HttpMethod.GET, "/health/1")
.noContent(HttpMethod.GET, "/health/1?deep=true")
.noContent(HttpMethod.GET, "/health/1?deep=true&dryRun=true")
.ok(HttpMethod.GET, "/health/generic", "{}");
@ -83,6 +84,13 @@ public class SpringContractTest { @@ -83,6 +84,13 @@ public class SpringContractTest {
Arrays.asList("application/json")));
}
@Test
public void composedAnnotation() {
resource.check("1");
mockClient.verifyOne(HttpMethod.GET, "/health/1");
}
@Test
public void notAHttpMethod() {
thrown.expectMessage("is not a method handled by feign");
@ -115,6 +123,9 @@ public class SpringContractTest { @@ -115,6 +123,9 @@ public class SpringContractTest {
@RequestParam(value = "deep", defaultValue = "false") boolean deepCheck,
@RequestParam(value = "dryRun", defaultValue = "false") boolean dryRun);
@GetMapping(value = "/{id}")
public void check(@PathVariable("id") String campaignId);
@ResponseStatus(value = HttpStatus.NOT_FOUND,
reason = "This customer is not found in the system")
@ExceptionHandler(MissingResourceException.class)

Loading…
Cancel
Save