RestTemplate เป็นไคลเอนต์ที่ให้บริการโดยฤดูใบไม้ผลิสำหรับการเข้าถึงบริการ REST RestTemplate มีวิธีที่สะดวกสบายในการเข้าถึงบริการ HTTP ระยะไกลซึ่งสามารถปรับปรุงประสิทธิภาพการเขียนของลูกค้าได้อย่างมาก ในบล็อกก่อนหน้า //www.vevb.com/article/132885.htm ไคลเอนต์เจอร์ซีย์ถูกนำมาใช้เพื่อใช้บริการพักผ่อนที่ใช้สปริงบูต ต่อไปเราใช้ RestTemplate เพื่อใช้บริการ restful ในตัวอย่างก่อนหน้า ตัวอย่างก่อนหน้า:
Springboot รวมฐานข้อมูลหน่วยความจำ H2 เพื่อให้ได้การทดสอบหน่วยและการไม่เกี่ยวข้องกับฐานข้อมูล
บริการ restful ที่จัดทำโดยตัวอย่างนี้มีดังนี้: http: // localhost: 7900/ผู้ใช้/1
{"id": 1, "ชื่อผู้ใช้": "user1", "ชื่อ": "จางซาน", "อายุ": 20, "ยอดคงเหลือ": 100.00}
การพึ่งพาไฟล์ POM มีดังนี้:
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/ XSI: schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" <ArtIfactId> SpringBoot-Rest-Template </ArtifactId> <sersion> 0.0.1-Snapshot </เวอร์ชัน> <packaging> jar </packaging> <name> springboot-rest-template </name> <scription> โครงการ Demo สำหรับสปริง <ArtIfactId> Spring-Boot-Starter-Parent </artifactid> <sersion> 1.4.3.release </เวอร์ชัน> <inelypath/> <!-ผู้ปกครองค้นหาจากที่เก็บ-> </parent> <properties> <project.build.sourceencoding> UTF-8 <Project.Reporting.UutputEncoding> UTF-8 </Project.Reporting.UutputUntEncoding> <Java.Version> 1.7 </Java.Version> </premerties> <การพึ่งพา> <การพึ่งพา> </permentency> <predency> <roupId> org.springframework.boot </groupid> <ratifactid> การทดสอบสปริง-สตาร์เทสต์ </artifactid> <pope> ทดสอบ </cope> </การพึ่งพา> <!-การบูตร้อน <RoupID> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Devtools </artifactid> <pontrail> True </onftional> </การพึ่งพาอาศัย> </การพึ่งพา <ArtIfactId> Spring-Boot-Maven-Plugin </artifactid> </plugin> </plugins> </uffer
เนื่องจากเราจำเป็นต้องแปลงสตริง JSON ที่เกี่ยวข้องเป็นวัตถุผู้ใช้หลังจากที่เราใช้ RestTemplate เพื่อเรียกใช้บริการ RESTFUL เราจึงต้องคัดลอกคลาสนี้ไปยังโครงการดังนี้:
แพ็คเกจ com.chhliu.springboot.restful.vo; นำเข้า java.math.bigdecimal; ผู้ใช้ระดับสาธารณะ {ID ส่วนตัวยาว; ชื่อผู้ใช้สตริงส่วนตัว; ชื่อสตริงส่วนตัว; อายุสั้นส่วนตัว; ยอดคงเหลือส่วนตัว // ... ละเว้นวิธีการ getter และ setter/ ** * ความสนใจ: * รายละเอียด: todo * @author chhliu * เวลาการสร้าง: 2017-1-20 2:05:45 PM * @return */ @Override สตริงสาธารณะ toString () {return "[id =" + id + ", username =" - - แพ็คเกจ 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 RESTTEMPLATE RESTTEMPLATE; @getMapping ("/template/{id}") ผู้ใช้สาธารณะ findById (@PathVariable Long ID) {// http: // localhost: 7900/ผู้ใช้/เป็น URL ที่สอดคล้องกันของผู้ใช้บริการก่อนหน้า u = this.resttemplate.getForObject ("http: // localhost: 7900/ 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 {// ระวังเมื่อเริ่มต้นว่าเนื่องจากเราฉีด RestTemplate ลงในคอนโทรลเลอร์เราจำเป็นต้องยกตัวอย่างอินสแตนซ์ของคลาสเมื่อเริ่ม @Autowired Private RestTemplateBuilder Builder; // ใช้ RestTemplateBuilder เพื่อยกตัวอย่างวัตถุ RestTemplate สปริงได้ฉีดอินสแตนซ์ RestTemplate โดยค่าเริ่มต้น @Bean RestTemplate RestTemplate () {return builder.build (); } โมฆะคงที่สาธารณะหลัก (สตริง [] args) {springapplication.run (springbootresttemplateapplication.class, args); - ป้อน: http: // localhost: 7902/เทมเพลต/1 ในเบราว์เซอร์ของคุณ
ผลการทดสอบมีดังนี้:
ผลการพิมพ์คอนโซล:
ผู้ใช้ [id = 1, ชื่อผู้ใช้ = user1, name = zhang san, อายุ = 20, balance = 100.00]
จากการทดสอบข้างต้นแสดงให้เห็นว่าเราได้เรียกใช้บริการ Spring Boot Restful สำเร็จ
มีสิ่งที่แย่มากในการทดสอบข้างต้น
ผู้ใช้ u = this.resttemplate.getForObject ("http: // localhost: 7900/ผู้ใช้/" + id, user.class); รหัสยากปรากฏขึ้นที่นี่ เมื่อที่อยู่เซิร์ฟเวอร์เปลี่ยนแปลงรหัสที่เกี่ยวข้องจะต้องมีการเปลี่ยนแปลงและต้องมีวิธีการปรับปรุงเพื่อเขียนที่อยู่ของบริการ RESTFUL ลงในไฟล์การกำหนดค่า
ปรับเปลี่ยนคอนโทรลเลอร์ดังนี้:
แพ็คเกจ 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 RESTTEMPLATE RESTTEMPLATE; // ที่อยู่ URL ที่สอดคล้องกับบริการ restful @Value ("$ {user.userServicePath}") สตริงส่วนตัว userserVicePath; @getMapping ("/template/{id}") ผู้ใช้สาธารณะ findById (@PathVariable Long ID) {user U = this.resttemplate.getForObject (this.userServicePath + id, user.class); System.out.println (u); กลับคุณ; -ไฟล์กำหนดค่าได้รับการแก้ไขดังนี้:
Server.port: 7902 user.userServicePath = http: // localhost: 7900/ผู้ใช้/
เริ่มโปรแกรม:
ฉันพบว่าการทดสอบนั้นโอเค ต่อมาเราจะแนะนำ Spring Cloud เพื่อปรับปรุงวิธีการโทรนี้ต่อไป!
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น