ฉันได้เขียนตัวอย่างการใช้งาน RestTemplate ตั้งแต่ฤดูใบไม้ผลิ 5 แนะนำปฏิกิริยาตอบโต้อย่างเต็มที่และยังมี WebClient ของ RestTemplate เวอร์ชันปฏิกิริยาบทความนี้จะแสดงการใช้งานพื้นฐานของ webclient
ขอให้พกพาส่วนหัว
พกคุกกี้
@Test โมฆะสาธารณะ testWithCookie () {mono <String> resp = webClient.create () .method (httpmethod.get) .uri ("http://baidu.com") .cookie ("โทเค็น", "xxxx") .BodyTomono (String.class); logger.info ("ผลลัพธ์: {}", resp.block ()); - พก auth พื้นฐาน
@Test โมฆะสาธารณะ testWithBasicauth () {String BasicAuth = "BASIC"+ BASE64.GETENCODER (). ENCODETOSTRING ("ผู้ใช้: PWD" .getBytes (StandardCharSets.UTF_8)); logger.info (basicauth); Mono <String> resp = webClient.create () .get () .uri ("http://baidu.com") .header (httpheaders.authorization, basicauth). retrieve () .bodytomono (string.class); logger.info ("ผลลัพธ์: {}", resp.block ()); - ตั้งค่าตัวแทนผู้ใช้ทั่วโลก
@Test โมฆะสาธารณะ TestWithHeaderFilter () {WebClient WebClient = WebClient.builder () .defaultheader (httpheaders.user_agent, "Mozilla/5.0 (Macintosh; Intel Mac OS x 10_12_6) Applewebkit/537.36 Chrome/63.0.3239.132 Safari/537.36 ") .filter (ExchangeFilterFunctions. Basicauthentication (" ผู้ใช้ "," รหัสผ่าน ")) .filter ((clientrequest, next) -> {logger.info clientRequest.headers (). foreach ((ชื่อ, ค่า) -> values.foreach (ค่า -> logger.info ("{} = {}", ชื่อ, ค่า))); Mono <String> resp = webClient.get () .uri ("https://baidu.com") .retrieve () .bodytomono (String.class); logger.info ("ผลลัพธ์: {}", resp.block ()); -รับคำขอ
ผ่านพารามิเตอร์โดยใช้ตัวยึดตำแหน่ง
@Test โมฆะสาธารณะ testurlplaceholder () {mono <String> resp = webClient.create () .get () // พารามิเตอร์หลายตัวสามารถวางได้โดยตรงในแผนที่และชื่อพารามิเตอร์สอดคล้องกับตัวยึดตำแหน่ง uri ("http://ww.baidu.com/s?wd= ตัวยึดตำแหน่ง retrieve () .bodytomono (string.class); logger.info ("ผลลัพธ์: {}", resp.block ()); - ผ่านพารามิเตอร์โดยใช้ uribuilder
@Test โมฆะสาธารณะ testUrlBiulder () {mono <String> resp = webClient.create () .get () .uri (uribuilder -> uribuilder .scheme ("http") .host ("www.baidu.com"). "ทดสอบ") .build ()) .retrieve () .bodytomono (string.class); logger.info ("ผลลัพธ์: {}", resp.block ()); -รูปแบบโพสต์
@Test โมฆะสาธารณะ Public TestFormParam () {MultivalUemap <String, String> FormData = ใหม่ LinkedMultivalUemap <> (); formdata.add ("name1", "value1"); formdata.add ("name2", "value2"); Mono <String> resp = webclient.create (). post () .uri ("http://www.w3school.com.cn/test/demo_form.asp") .contenttype (mediaType.application_form_urlencoded) .Retrieve (). BodyTomono (String.class); logger.info ("ผลลัพธ์: {}", resp.block ()); -โพสต์ JSON
ใช้ถั่วเพื่อโพสต์
หนังสือคลาสคงที่ {ชื่อสตริง; ชื่อสตริง; สตริงสาธารณะ getName () {ชื่อคืน; } โมฆะสาธารณะ setName (ชื่อสตริง) {this.name = name; } สตริงสาธารณะ getTitle () {return title; } โมฆะสาธารณะ settitle (ชื่อสตริง) {this.title = title; }} @Test โมฆะสาธารณะ testPostJSON () {หนังสือหนังสือ = หนังสือเล่มใหม่ (); book.setName ("ชื่อ"); book.settitle ("นี่คือชื่อ"); Mono <String> resp = webClient.create (). post () .uri ("http: // localhost: 8080/demo/json") .contenttype (mediaType.application_json_utf8) .body (mono.just (หนังสือ), book.class). logger.info ("ผลลัพธ์: {}", resp.block ()); - โพสต์โดยตรง json ดิบ
@Test โมฆะสาธารณะ TestPostrawjson () {mono <String> resp = webClient.create (). post () .uri ("http: // localhost: 8080/demo/json") .contenttype (mediaType.application_json_utf8). Title/",/n" + "/" ผู้แต่ง/":/" นี่คือผู้แต่ง/"/n" + "}")) .retrieve (). BodyTomono (String.class); logger.info ("ผลลัพธ์: {}", resp.block ()); -โพสต์ไฟล์ binary-upload
@Test โมฆะสาธารณะ TestupLoadFile () {HTTPHEADERS Headers = HTTPHEADERS ใหม่ (); Headers.SetContentType (MediaType.image_png); HTTPENTITY <SCLOSTATHRESOURCE> Entity = ใหม่ httPentity <> (ใหม่ classPathResource ("parallel.png"), ส่วนหัว); MultivalUemap <String, Object> parts = new LinkedMultivalUemap <> (); parts.add ("ไฟล์", เอนทิตี); Mono <String> resp = webClient.create (). post () .uri ("http: // localhost: 8080/upload") .contenttype (mediaType.multipart_form_data) .body logger.info ("ผลลัพธ์: {}", resp.block ()); -ดาวน์โหลดไบนารี
ดาวน์โหลดรูปภาพ
@Test โมฆะสาธารณะ testDownloadImage () พ่น IOException {mono <resource> resp = webclient.create (). get () .uri ("http://www.toolip.gr/captcha?complexity=99&size=60&length=9" .Retrieve (). BodyTomono (Resource.class); ทรัพยากรทรัพยากร = resp.block (); bufferedImage bufferedImage = imageio.read (Resource.getInputStream ()); Imageio.write (bufferedImage, "png", ไฟล์ใหม่ ("captcha.png")); - ดาวน์โหลดไฟล์
@Test โมฆะสาธารณะ testdownloadfile () พ่น IOException {mono <clientResponse> resp = webClient.create (). get () .uri ("http: // localhost: 8080/ไฟล์/ดาวน์โหลด") การตอบสนอง clientResponse = resp.block (); การจัดการสตริง = response.headers (). ashttpheaders (). getfirst (httpheaders.content_disposition); String filename = disposition.substring (disposition.indexof ("=")+1); ทรัพยากรทรัพยากร = response.bodytomono (Resource.class) .block (); ไฟล์ out = ไฟล์ใหม่ (ชื่อไฟล์); fileutils.copyInputStreamTofile (Resource.GetInputStream (), ออก); logger.info (out.getabsolutepath ()); -การจัดการข้อผิดพลาด
@Test โมฆะสาธารณะ testRetrieve4xx () {webClient webClient = webClient.builder () .baseurl ("https://api.github.com") .defaultheader (httpheaders.content_type .defaultheader (httpheaders.user_agent, "Spring 5 Webclient") .build (); WebClient.ResponsEspec ResponseSpec = WebClient.Method (httpmethod.get) .uri ("/ผู้ใช้/repos? sort = {sortfield} & direction = {sortDirection}", "อัปเดต", "desc") Mono <String> mono = responsespec .onstatus (e -> e.is4xxClientError (), resp -> {logger.error ("ข้อผิดพลาด: {}, msg: {}", resp.statuscode () ค่า () + ":" + resp.statuscode (). getReasonPhrase ()); RuntimeException (err.getMessage ()); สตริงผลลัพธ์ = mono.block (); logger.info ("ผลลัพธ์: {}", ผลลัพธ์); -สรุป
WebClient เป็นเทมเพลต REST ASYNC รุ่นใหม่และ API นั้นค่อนข้างง่ายและมีปฏิกิริยาซึ่งคุ้มค่ามากที่ใช้
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น