RestTemplate هو عميل يقدمه الربيع للوصول إلى خدمات REST. يوفر RestTemplate مجموعة متنوعة من الطرق المريحة للوصول إلى خدمات HTTP عن بُعد ، والتي يمكن أن تحسن بشكل كبير من كفاءة كتابة العميل. في المدونة السابقة ، //www.vevb.com/article/132885.htm ، تم استخدام عميل Jersey لتنفيذ الخدمة المريحة التي تستهلك SPRING Boot. بعد ذلك ، نستخدم RestTemplate لاستهلاك الخدمة المريحة في المثال السابق. المثال السابق:
يدمج Springboot قاعدة بيانات ذاكرة H2 لتحقيق اختبارات الوحدة وعدم صلة قاعدة البيانات
الخدمات المريحة التي يقدمها هذا المثال هي كما يلي: http: // localhost: 7900/user/1
{"id": 1 ، "username": "user1" ، "name": "Zhang San" ، "Age": 20 ، "Balance": 100.00}
تبعيات ملف POM هي كما يلي:
<؟ XSI: schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <StifactId> Springboot-Rest-Template </shintifactid> <soph> 0.0.1-snapshot </spleice> <cplaging> جرة </packaging> <name> springboot-rest-template </name> <scription> Demo Project for Spring Boot Resttemplate </description> <StifactId> Spring-Boot-Starter-Parent </stifactid> <الإصدار> 1.4.3.Release </version> <repiledpath/> <!-Lookup Parent من المستودع-> </parent> <ferties> <project.build.sourceencoding> usf-8 </proceld.build.source <project.reporting.outputencoding> utf-8 </project.reporting.outputencoding> <java.version> 1.7 </java.version> </properties> <ependencies> <redenced> <roupend> org.springframework.boot </groupid> <Rependency> <roupeD> org.springframework.boot </rougiD> <intifactid> Spring-Boot-Starter-Test </attifactid> <scope> اختبار </scope> </sependency> <!-الحذاء الساخن ، حزمة التبعية الساخنة ، للحصول على سلامة التصحيح ، إضافة هذه الحزمة- <StifactId> spring-boot-devtools </intifactid> <اختياري> صحيح </اختياري> </repreadency> </repreadencies> <buy> <ultionins> <culsin> <clupin> <rougiD> org.springframework.boot </rougiDid> </project>
نظرًا لأننا بحاجة إلى تحويل سلسلة JSON المقابلة إلى كائن مستخدم بعد استخدام RestTemplate للاتصال بالخدمة المريحة ، نحتاج إلى نسخ هذه الفئة إلى المشروع ، على النحو التالي:
حزمة com.chhliu.springboot.restful.vo ؛ استيراد java.math.bigdecimal ؛ مستخدم الفئة العامة {private long id ؛ اسم المستخدم الخاص بالسلسلة الخاصة ؛ اسم السلسلة الخاصة ؛ العمر القصير الخاص ؛ توازن كبير خاص. // ... حذف أساليب getter و setter/ ** * الانتباه: * التفاصيل: toDo * author chhliu * وقت الإنشاء: 2017-1-20 2:05:45 pm * @return */ Override public string toString () "]" ؛ }} حزمة com.chhliu.springboot.restful.controller ؛ استيراد org.springframework.beans.factory.annotation.autowired ؛ استيراد 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 الفئة العامة RestTemplateController {autowired private resttemplate resttemplate ؛ getMapping ("/template/{id}") مستخدم عام findbyid (@pathvariable معرف طويل) {// http: // localhost: 7900/user/هو عنوان URL المقابل للمستخدم السابق u = this.resttemplate.getforObject ("http: // localhost: 7900/user System.out.println (u) ؛ أعدك ؛ }} حزمة com.chhliu.springboot.restful ؛ استيراد org.springframework.beans.factory.annotation.autowired ؛ استيراد org.springframework.boot.springapplication ؛ استيراد org.springframework.boot.autoconfigure.SpringBootApplication ؛ استيراد org.springframework.boot.web.client.restTemplateBuilder ؛ استيراد org.springframework.context.annotation.bean ؛ استيراد org.springframework.web.client.restTemplate ؛ springbootapplication الفئة العامة springbootresttemplateapplication {// كن حذرا عند البدء في ذلك لأننا حقن rettemplate في وحدة التحكم ، نحتاج إلى إنشاء مثيل للفصل عند بدء resttemplateBuilder الخاص autowired ؛ // استخدم RestTemplateBuilder لتشكيل كائن RestTemplate. حقن Spring مثيل RestTemplate بشكل افتراضي RestTemplate RestTemplate () {return builder.build () ؛ } public static void main (string [] args) {springapplication.run (SpringBootRestTemplateApplication.class ، args) ؛ }} أدخل: http: // localhost: 7902/template/1 في متصفحك
نتائج الاختبار كما يلي:
نتائج طباعة وحدة التحكم:
المستخدم [معرف = 1 ، اسم المستخدم = user1 ، الاسم = Zhang SAN ، العمر = 20 ، التوازن = 100.00]
من خلال الاختبار أعلاه ، يظهر أننا قد أطلقنا على خدمة Restful Spring Boot بنجاح.
هناك شيء سيء للغاية في الاختبار أعلاه.
user u = this.restTemplate.getForObject ("http: // localhost: 7900/user/" + id ، user.class) ؛ يظهر الرمز الصلب هنا. عندما يتغير عنوان الخادم ، يجب تغيير الرمز المقابل ، ويجب أن تكون هناك طريقة محسّنة لكتابة عنوان الخدمة المريحة في ملف التكوين.
تعديل وحدة التحكم على النحو التالي:
حزمة com.chhliu.springboot.restful.controller ؛ استيراد org.springframework.beans.factory.annotation.autowired ؛ استيراد org.springframework.beans.factory.annotation.value ؛ استيراد 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 الفئة العامة RestTemplateController {autowired private resttemplate resttemplate ؛ . getMapping ("/template/{id}") المستخدم العام findById (@pathvariable معرف طويل) {user u = this.restTemplate.getForObject (this.userservicepath + id ، user.class) ؛ System.out.println (u) ؛ أعدك ؛ }}يتم تعديل ملف التكوين على النحو التالي:
server.port: 7902 user.userservicepath = http: // localhost: 7900/user/
بدء البرنامج:
لقد وجدت أن الاختبار على ما يرام. في وقت لاحق ، سوف نقدم Spring Cloud لتحسين طريقة الاتصال هذه!
ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.