開発環境:のIntelliJ IDEA 2019年2月2日
春ブートバージョン:2.1.8
まず、RESTサービスを公開
1、春ブーツの残りのサーバープロジェクトの新しい名前を作成するために、IDEA
2、新しいエンティティクラスUser.cs
パッケージcom.example.restserver.domain。 パブリック クラスユーザー{ 文字列名。 整数の年齢; パブリック文字列のgetName(){ 戻り名。 } 公共 ボイドのsetName(文字列名){ この .nameの= 名前。 } パブリック整数getAge(){ 戻り年齢; } 公共 ボイドsetAge(整数年齢){ この .age = 年齢。 } }
2、新しいコントローラクラスUserController.cs
パッケージcom.example.restserver.web。 輸入com.example.restserver.domain.User。 輸入org.springframework.http.MediaType。 輸入org.springframework.web.bind.annotation.PathVariable; 輸入org.springframework.web.bind.annotation.RequestMapping。 輸入org.springframework.web.bind.annotation.RestController。 @RestController パブリック クラスUserControllerで{ @RequestMapping(値 = "/ユーザ/ {名}"、=生成MediaType.APPLICATION_JSON_VALUE)を パブリックユーザユーザ(@PathVariable文字列名){ ユーザU = 新しいユーザー(); u.setName(名前); u.setAge( 30 )。 返すuと。 } }
次のようにプロジェクトが構成されています。
訪問にhttp:// localhost:8080 /ユーザー/ LC、ページの表示:
{ "名": "LC"、 "年齢":30}
第二に、使用RestTemplaeコールサービス
1、春ブーツに残り、クライアントのプロジェクトのための新しい名前を作成するために、IDEA
2、新しいクラスは、通常のRestTemplateMain.csの主な方法、コールサービスが含まれています
パッケージcom.example.restclient。 輸入com.example.restclient.domain.User。 輸入org.springframework.web.client.RestTemplate。 パブリック クラスRestTemplateMain { 公共 静的 ボイドメイン(文字列[]引数){ RestTemplateのTPL = 新しいRestTemplate()。 ユーザU = tpl.getForObject( "にhttp:// localhost:8080 /ユーザー/ LC"。、ユーザークラス)。 System.out.println(u.getName() + "" + u.getAge()); } }
右のファイル名を指定して実行 'RestTemplateMain.main()'、コンソール出力:LC、30
3、RestTemplateBuilder、新しいクラスUserService.csを使用することができる豆RestTemplateを使用
パッケージcom.example.restclient.service。 輸入com.example.restclient.domain.User。 輸入org.springframework.beans.factory.annotation.Autowired; 輸入org.springframework.boot.web.client.RestTemplateBuilder。 輸入org.springframework.context.annotation.Bean。 輸入org.springframework.stereotype.Service。 輸入org.springframework.web.client.RestTemplate。 @Service パブリック クラスUserServiceの{ @Autowired プライベート; RestTemplateBuilderビルダー @Bean 公共RestTemplate restTemplate(){ リターンbuilder.rootUri( "のhttp:// localhostを:8080" ).build(); } パブリックユーザuserBuilder(文字列名){ ユーザU = restTemplate()getForObject( "/ユーザ/" +名、ユーザ。。クラス)。 返すuと。 } }
図4に示すように、ライトユニットテストクラスは、上記豆UserServiceのをテストします。
パッケージcom.example.restclient.service。 輸入com.example.restclient.domain.User。 輸入org.junit.Assert。 輸入org.junit.Test; 輸入org.junit.runner.RunWith; 輸入org.springframework.beans.factory.annotation.Autowired; 輸入org.springframework.boot.test.context.SpringBootTest; 輸入org.springframework.test.context.junit4.SpringRunner。 @RunWith(SpringRunner。クラス) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) パブリック クラスUserServiceTest { @Autowired 民間UserServiceのUserServiceの; @Test 公共 ボイドTESTUSER(){ ユーザU = userService.userBuilder( "LC" )。 Assert.assertEquals( "LC" 、u.getName()); } }
図5に示すように、コントローラクラスUserController.csコール
そして、ポートは8080はserver.port = 9001と同じではありませんapplication.propertiesに配置されました
@Autowired プライベートUserServiceのUserServiceの。 @RequestMapping(値 = "/ユーザ/ {名前}"、生産= MediaType.APPLICATION_JSON_VALUE) パブリックユーザユーザ(@PathVariable文字列名){ ユーザU = userService.userBuilder(名); 返すuと。 }
第三に、使用装うコールサービス
残りのクライアントに基づいてプロジェクトにコードを変更し続けています。
1、のpom.xml依存関係を追加
< 依存性> < のgroupId > io.github.openfeign </ のgroupId > < たartifactId >ふりコア</ たartifactId > < バージョン> 9.5.0 </ バージョン> </ 依存> < 依存性> < のgroupId > io.github.openfeign </ groupIdを> < たartifactId >装う-gson </ たartifactId > < バージョン>9.5.0 </バージョン> </ 依存関係>
2、新しいインターフェイスUserClient.cs
パッケージcom.example.restclient.service。 輸入com.example.restclient.domain.User。 輸入feign.Param。 輸入feign.RequestLine。 パブリック インターフェースUserClient { @RequestLine( "GET /ユーザ/ {名}" ) ユーザーのgetUser(@Param( "名前" )文字列名)。 }
図3に示すように、呼制御クラスUserController.cs
@RequestMapping(値= "/ USER2 / {名前}"、生産= MediaType.APPLICATION_JSON_VALUE) パブリックユーザuser2(@PathVariable文字列名){ UserClientサービス = Feign.builder()。デコーダ(新しいGsonDecoder()) .TARGET(UserClient 。クラス、 "にhttp:// localhost:8080 /" ); ユーザU = service.getUser(名); 返すuと。 }
図4に示すように、第三の最適化は、構成ファイルへのリクエストのコードとアドレスステップ。
(1)設定を追加application.properties
application.client.url =のhttp:// localhostを:8080
(2)ClientConfig.cs基づいて新しいプロファイルを作成します
パッケージcom.example.restclient.config。 輸入com.example.restclient.service.UserClient。 輸入feign.Feign。 輸入feign.gson.GsonDecoder。 輸入org.springframework.beans.factory.annotation.Value。 輸入org.springframework.context.annotation.Bean。 輸入org.springframework.context.annotation.Configuration。 @Configuration パブリック クラスClientConfig { @value( "$ {application.client.url}" ) プライベート文字列clientUrl。 @Bean UserClient userClient(){ UserClientクライアント =Feign.builder() .decoder(新しいGsonDecoder()) .TARGET(UserClient。クラス、clientUrl)。 返すクライアントを。 } }
(3)コントローラUserController.csコール
@Autowired プライベート UserClient userClient。 @RequestMapping(値 = "/ USER3 / {名前}"、生産= MediaType.APPLICATION_JSON_VALUE) パブリックユーザUSER3(@PathVariable文字列名){ ユーザU = userClient.getUser(名); 返すuと。 }
UserController.cs最終コンテンツ:
パッケージcom.example.restclient.web。 輸入com.example.restclient.domain.User。 輸入com.example.restclient.service.UserClient。 輸入com.example.restclient.service.UserService。 輸入feign.Feign。 輸入feign.gson.GsonDecoder。 輸入org.springframework.beans.factory.annotation.Autowired; 輸入org.springframework.http.MediaType。 輸入org.springframework.web.bind.annotation.PathVariable; 輸入org.springframework.web.bind.annotation.RequestMapping。 輸入org.springframework.web.bind.annotation.RestController。 @RestController 公衆 クラスUserControllerで{ @Autowired プライベートUserServiceのUserServiceのを。 @Autowired プライベート UserClient userClient。 @RequestMapping(値 = "/ユーザ/ {名前}"、生産= MediaType.APPLICATION_JSON_VALUE) パブリックユーザユーザ(@PathVariable文字列名){ ユーザU = userService.userBuilder(名); 返すuと。 } @RequestMapping(値 = "/ USER2 / {名前}"、=生成MediaType.APPLICATION_JSON_VALUE) パブリックユーザuser2(@PathVariable文字列名){ UserClientサービス = Feign.builder()。デコーダ(新しいGsonDecoderを()) 。.TARGET(UserClient クラス、 "にhttp:// localhost:8080 /" ); ユーザU = service.getUser(名); 返すuと。 } @RequestMapping(値 = "/ USER3 / {名前}"、=生成MediaType.APPLICATION_JSON_VALUE)を パブリックユーザUSER3(@PathVariable文字列名){ ユーザU = userClient.getUser(名); 返すuと。 } }
プロジェクト構造
下記のアドレスを訪れ、あなたは、通常の出力の結果を見ることができます
http:// localhostを:9001 /ユーザー/ LCの
のhttp:// localhostを:9001 / USER2 / LC2の
のhttp:// localhostを:9001 /ユーザー3 / LC3