{"id":109,"date":"2018-09-10T20:02:38","date_gmt":"2018-09-10T20:02:38","guid":{"rendered":"http:\/\/www.matez.de\/?p=109"},"modified":"2018-09-10T20:05:16","modified_gmt":"2018-09-10T20:05:16","slug":"response-validation-when-using-feign-as-http-client-wrapper","status":"publish","type":"post","link":"https:\/\/www.matez.de\/index.php\/2018\/09\/10\/response-validation-when-using-feign-as-http-client-wrapper\/","title":{"rendered":"Response validation when using Feign as http client wrapper"},"content":{"rendered":"<p>If you have the requirement of validating the response of a rest interface, a good opportunity in Java is to use the bean validation api and its annotations. On top, if you use <a href=\"https:\/\/github.com\/OpenFeign\/feign\">OpenFeign<\/a> wrapper to define the client in your application, you can now assign those validation annotations directly to your interface.<\/p>\n<p>If you have already seen in my previous blogpost about <a href=\"https:\/\/www.matez.de\/index.php\/2017\/10\/29\/feign-outbound-metrics\/\">Feign Outbound metrics<\/a>, I am trying to extend feign with some decorator classes to add missing functionality. <a href=\"https:\/\/github.com\/mwiede\/metrics-feign\">metrics-feign<\/a> is a library, which can be used to report outbound-metrics out of any invocation to the feign wrapper.<\/p>\n<p>Now I created another library, which makes it possible to validate the response using bean validation api (JSR349). Its called <a href=\"https:\/\/github.com\/mwiede\/feign-validation\">feign-validation<\/a> and you can find it at <a href=\"https:\/\/github.com\/mwiede\/feign-validation\">github<\/a> or <a href=\"https:\/\/search.maven.org\/search?q=a:feign-validation\">maven central<\/a>.<\/p>\n<p>The only thing you have to do after setting it up is to add annotations to the interface class you are using. Here is an example:<\/p>\n<pre>interface GitHub {\r\n@RequestLine(\"GET \/repos\/{owner}\/{repo}\/contributors\")\r\n@Size(min = 1, max = 3)\r\n@Valid\r\nList contributors(@Param(\"owner\") String owner, @Param(\"repo\") String repo);\r\n\r\n}\r\n\r\nstatic class Contributor {\r\n@NotNull\r\n@Size(min = 10)\r\nString login;\r\nint contributions;\r\n}\r\n\r\npublic static void main(String... args) {\r\n\r\nValidator validator = Validation.buildDefaultValidatorFactory().getValidator();\r\n\r\nGitHub github = ExtendedFeign.builder(validator)\/\/\r\n.decoder(new GsonDecoder()).target(GitHub.class, \"https:\/\/api.github.com\");\r\n\r\n\/\/ Fetch and print a list of the contributors to this library.\r\ntry {\r\nList contributors = github.contributors(\"OpenFeign\", \"feign\");\r\nfor (Contributor contributor : contributors) {\r\nSystem.out.println(contributor.login + \" (\" + contributor.contributions + \")\");\r\n}\r\n} catch (ConstraintViolationException ex) {\r\nex.getConstraintViolations().forEach(System.out::println);\r\n}\r\n\r\n}<\/pre>\n<p>If you would run this class, you can see, that the response is being validated against the given annotations and all violations a printed to stdout.<\/p>\n<ul>\n<li>First, the @Size annotation leads to a constraint violation, because the size of the list is checked, whether it has at least one or maximim three items.<\/li>\n<li>Because of @Valid, each item in the list of contributors is validated<\/li>\n<li>@Size in Contributor class is set to verify, that the login name must have at least 10 characters. Because not all contributors have such a long name, a few more violations are reported<\/li>\n<li>as seen, you can use any annotation from the api to fulfil the need<\/li>\n<\/ul>\n<p>So what are you waiting for?<\/p>\n<ul>\n<li>add\n<pre>&lt;dependency&gt;\r\n  &lt;groupId&gt;com.github.mwiede&lt;\/groupId&gt;\r\n  &lt;artifactId&gt;feign-validation&lt;\/artifactId&gt;\r\n  &lt;version&gt;1.0&lt;\/version&gt;\r\n&lt;\/dependency&gt;<\/pre>\n<p>and<\/p>\n<pre>&lt;dependency&gt;\r\n    &lt;groupId&gt;org.hibernate&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;hibernate-validator&lt;\/artifactId&gt;\r\n    &lt;version&gt;5.4.2.Final&lt;\/version&gt;\r\n&lt;\/dependency&gt;<\/pre>\n<p>to your dependencies<\/li>\n<li>exchange\u00a0<i>feign.Feign.builder\u00a0<\/i>with\u00a0<i>com.github.mwiede.feign.validation.ExtendedFeign.builder<\/i><\/li>\n<li>and add annotations like explained above.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>If you have the requirement of validating the response of a rest interface, a good opportunity in Java is to use the bean validation api and its annotations. On top, if you use OpenFeign wrapper to define the client in your application, you can now assign those validation annotations directly to your interface. If you &hellip; <a href=\"https:\/\/www.matez.de\/index.php\/2018\/09\/10\/response-validation-when-using-feign-as-http-client-wrapper\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Response validation when using Feign as http client wrapper&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[30,3,31,29],"_links":{"self":[{"href":"https:\/\/www.matez.de\/index.php\/wp-json\/wp\/v2\/posts\/109"}],"collection":[{"href":"https:\/\/www.matez.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.matez.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.matez.de\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.matez.de\/index.php\/wp-json\/wp\/v2\/comments?post=109"}],"version-history":[{"count":4,"href":"https:\/\/www.matez.de\/index.php\/wp-json\/wp\/v2\/posts\/109\/revisions"}],"predecessor-version":[{"id":115,"href":"https:\/\/www.matez.de\/index.php\/wp-json\/wp\/v2\/posts\/109\/revisions\/115"}],"wp:attachment":[{"href":"https:\/\/www.matez.de\/index.php\/wp-json\/wp\/v2\/media?parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.matez.de\/index.php\/wp-json\/wp\/v2\/categories?post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.matez.de\/index.php\/wp-json\/wp\/v2\/tags?post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}