kagamihogeの日記

kagamihogeの日記です。

Spring Cloud OpenFeignさわる

https://spring.io/projects/spring-cloud-openfeign をさわる。

FeignというRESTクライアントとspringをいい感じに連携する、という代物らしい。

使ってみる

Spring Initializrでプロジェクトを作る。dependencyにopenfeignをいれる。あと、今回はレスポンスがxmlなのでそのための依存性も追加する。

plugins {
  id 'org.springframework.boot' version '2.2.7.RELEASE'
  id 'io.spring.dependency-management' version '1.0.9.RELEASE'
  id 'java'
}

sourceCompatibility = '11'
configurations {
  compileOnly {
    extendsFrom annotationProcessor
  }
}
repositories {
  mavenCentral()
}
ext {
  set('springCloudVersion', "Hoxton.SR4")
}
dependencies {
  implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
  implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml'
  compileOnly 'org.projectlombok:lombok'
  annotationProcessor 'org.projectlombok:lombok'
  testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  }
}
dependencyManagement {
  imports {
    mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
  }
}
test {
  useJUnitPlatform()
}

Feignのインタフェースとかmainとかを作る。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@SpringBootApplication
@EnableFeignClients
public class Application implements CommandLineRunner {
  @Autowired
  NicoThumbinfoClient nicoThumbinfoClient;

  @FeignClient(value = "zip", url = "${niconico.url}", configuration = CustomConfigration.class)
  static interface NicoThumbinfoClient {
    @RequestMapping(method = RequestMethod.GET, value = "/api/getthumbinfo/{sm}")
    public NicovideoThumbResponse get(@PathVariable("sm") String sm);
  }

  @Override
  public void run(String... args) throws Exception {
    System.out.println(nicoThumbinfoClient.get("sm9"));
  }

  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }

}

Feignは、上記のように、interfaceでクライアントを定義する。valueは適当なID、urlは後述のプロパティファイルで定義(直書きでも良い)、configurationxmlのデコード設定でこれも後述。@RequestMappingとか@PathVariableとかのノリは今までのspringと変わらない。

挙動確認のAPIニコニコ動画APIを使わせて頂いた。

src/main/resources/application.propertiesはこんな感じ。

niconico.url=ext.nicovideo.jp

以下はxmlをデコードするため設定クラス。

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;

import feign.codec.Decoder;

@Configuration
public class CustomConfigration {
  @Bean
  public Decoder feignDecoder() {
    MappingJackson2XmlHttpMessageConverter c = new MappingJackson2XmlHttpMessageConverter();
    ObjectFactory<HttpMessageConverters> objectFactory = () -> new HttpMessageConverters(c);
    return new ResponseEntityDecoder(new SpringDecoder(objectFactory));
  }
}

これも今までのspringと同じで適当なconverterを追加してやればよい。

最後に、結果格納用のクラス。

import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

import lombok.Data;

@Data
public class NicovideoThumbResponse {
  Thumb thumb;

  @Data
  public static class Thumb {
    @JacksonXmlProperty(localName = "video_id")
    String videoId;
    String title;
  }
}

これを実行すると、以下のように表示される。

NicovideoThumbResponse(thumb=NicovideoThumbResponse.Thumb(videoId=sm9, title=新・豪血寺一族 -煩悩解放 - レッツゴー!陰陽師))