RESTTEMPLATEは、RESTサービスにアクセスするためにSpringが提供するクライアントです。 RESTTEMPLATEは、リモートHTTPサービスにアクセスするためのさまざまな便利な方法を提供します。これにより、クライアントのライティング効率を大幅に改善できます。以前のブログ、//www.vevb.com/article/132885.htmでは、JerseyのクライアントがSpring Bootを消費するRestfulサービスを実装するために使用されています。次に、RestTemplateを使用して、前の例でRESTFULサービスを消費します。前の例:
SpringbootはH2メモリデータベースを統合してユニットテストとデータベースの無関係を実現します
この例で提供される安らかなサービスは次のとおりです。http:// localhost:7900/user/1
{"id":1、 "username": "user1"、 "name": "zhang san"、 "age":20、 "balance":100.00}
POMファイルの依存関係は次のとおりです。
<?xml version = "1.0" encoding = "utf-8"?> <project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance <http://www.w3.org/2001 xsi:schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.0.sdsd"> <modelversion> 4.0.0 </modelversion> <グループ<artifactid> springboot-rest-template </artifactid> <バージョン> 0.0.1-snapshot </version> <packaging> jar </packaging> <name> springboot-rest-template </name> <dastriction> spring boot retemplate </description> <parent> <artifactid> spring-boot-starter-parent </artifactid> <version> 1.4.3.release </version> <relativepath/> <! - lookup parent from repository-> </parent> <properties> <build.sourceencoding> utf-8 </project.build.sourceencoding> <project.reporting.outputencoding> utf-8 </project.reporting.outputencoding> <java.version> 1.7 </java.version> </properties> <dependencies> <shipencies> <dependency> <グループ<Dependency> groupId> org.springframework.boot </groupid> <artifactid> spring-boot-starter-test </artifactid> <scope>テスト</scope> </dependency> <! - ホットブート、ホットブート、ホットブート、ホット展開依存関係パッケージ、利便性のデバッグのため、このパッケージの追加<artifactid> spring-boot-devtools </artifactid> <optional> true </optional> </dependency> dependency> <bultins> <bultins> <plugin> <plugin> <groupid> org.springframework.boot </groupid> <artifactid> spring-brugin </plugintin </plugin </project>
RESTTEMPLATEを使用してRESTFULサービスを呼び出すと、対応するJSON文字列をユーザーオブジェクトに変換する必要があるため、次のようにこのクラスをプロジェクトにコピーする必要があります。
パッケージcom.chhliu.springboot.restful.vo; java.math.bigdecimalをインポートします。パブリッククラスユーザー{private long id;プライベート文字列ユーザー名;プライベート文字列名;プライベートショート年齢;プライベートビッグデキマルバランス。 // ... getter and setter Methods/ ** *注意: *詳細:todo * @author chhhliu *作成時間:2017-1-20 2:05:45 pm * @return */ @Override public string toString(){return "user [id =" + id + "、username =" + username + " }}パッケージcom.chhliu.springboot.restful.controller; Import org.springframework.beans.factory.annotation.autowired; Import org.springframework.web.bind.annotation.getMapping; org.springframework.web.bind.annotation.pathvariableをインポートします。 org.springframework.web.bind.annotation.restControllerをインポートします。 org.springframework.web.client.resttemplateをインポートします。 com.chhliu.springboot.restful.vo.userをインポートします。 @restController public class rettemplatecontroller {@autowired privateResttemplateRestTemplate; @getMapping( "/template/{id}")パブリックユーザーfindbyid(@pathvariable long id){// http:// localhost:7900/user/は、以前のサービスユーザーの対応するURLですu = this.resttemplate.getforobject( "http:// localhost:7900/user/" + " +" + " + id、class System.out.println(u); uを返します。 }}パッケージcom.chhliu.springboot.restful; Import org.springframework.beans.factory.annotation.autowired; Import org.springframework.boot.springApplication; Import org.springframework.boot.autoconfigure.springbootapplication; org.springframework.boot.web.client.resttemplateBuilderをインポートします。 org.springframework.context.annotation.beanをインポートします。 org.springframework.web.client.resttemplateをインポートします。 @SpringBootApplication Public Class SpringBootRestTemplateApplication {// RESTTEMPLATEをコントローラーに注入するため、それを開始するときは注意してください。 // RestTemplateBuilderを使用して、RestTemplateオブジェクトをインスタンス化します。 springは、デフォルトでrettemplateインスタンスを@bean public rettemplate resttemplate(){return builder.build(); } public static void main(string [] args){springApplication.run(springbootresttemplateapplication.class、args); }}入力:http:// localhost:7902/template/1ブラウザに
テスト結果は次のとおりです。
コンソール印刷結果:
user [id = 1、username = user1、name = zhang san、age = 20、残高= 100.00]
上記のテストを通じて、Spring Boot Restfulサービスと正常に呼ばれることが示されています。
上記のテストでは非常に悪いことがあります。
ユーザーu = this.resttemplate.getForObject( "http:// localhost:7900/user/" + id、user.class);
ハードコードがここに表示されます。サーバーアドレスが変更されると、対応するコードを変更する必要があり、REST Ful Serviceのアドレスを構成ファイルに書き込むには、改善された方法が必要です。
次のようにコントローラーを変更します。
パッケージcom.chhliu.springboot.restful.controller; Import org.springframework.beans.factory.annotation.autowired; Import org.springframework.beans.factory.annotation.value; Import org.springframework.web.bind.annotation.getMapping; org.springframework.web.bind.annotation.pathvariableをインポートします。 org.springframework.web.bind.annotation.restControllerをインポートします。 org.springframework.web.client.resttemplateをインポートします。 com.chhliu.springboot.restful.vo.userをインポートします。 @restController public class rettemplatecontroller {@autowired privateResttemplateRestTemplate; // restful serviceに対応するURLアドレス@value( "$ {user.userservicepath}")private string userservicepath; @getMapping( "/template/{id}")public user findbyid(@pathvariable long id){user u = this.resttemplate.getforobject(this.userservicepath + id、user.class); System.out.println(u); uを返します。 }}構成ファイルは次のように変更されます。
Server.port:7902 user.userservicepath = http:// localhost:7900/user//
プログラムを開始します:
テストは大丈夫であることがわかりました。その後、この呼び出し方をさらに改善するためにSpring Cloudを導入します!
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。