日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術(shù)文章
文章詳情頁

Spring Boot2發(fā)布調(diào)用REST服務(wù)實(shí)現(xiàn)方法

瀏覽:298日期:2023-09-09 09:26:51

開發(fā)環(huán)境:IntelliJ IDEA 2019.2.2Spring Boot版本:2.1.8

一、發(fā)布REST服務(wù)

1、IDEA新建一個(gè)名稱為rest-server的Spring Boot項(xiàng)目

2、新建一個(gè)實(shí)體類User.java

package com.example.restserver.domain;public class User { String name; Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; }}

3、新建一個(gè)控制器類 UserController.java

package com.example.restserver.web;import com.example.restserver.domain.User;import org.springframework.http.MediaType;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class UserController { @RequestMapping(value='/user/{name}', produces = MediaType.APPLICATION_JSON_VALUE) public User user(@PathVariable String name) { User u = new User(); u.setName(name); u.setAge(30); return u; }}

項(xiàng)目結(jié)構(gòu)如下:

Spring Boot2發(fā)布調(diào)用REST服務(wù)實(shí)現(xiàn)方法

訪問http://localhost:8080/user/lc,頁面顯示:

{'name':'lc','age':30}

二、使用RestTemplae調(diào)用服務(wù)

1、IDEA新建一個(gè)名稱為rest-client的Spring Boot項(xiàng)目

2、新建一個(gè)含有main方法的普通類RestTemplateMain.java,調(diào)用服務(wù)

package com.example.restclient;import com.example.restclient.domain.User;import org.springframework.web.client.RestTemplate;public class RestTemplateMain { public static void main(String[] args){ RestTemplate tpl = new RestTemplate(); User u = tpl.getForObject('http://localhost:8080/user/lc', User.class); System.out.println(u.getName() + ',' + u.getAge()); }}

右鍵Run ’RestTemplateMain.main()’,控制臺輸出:lc,30

3、在bean里面使用RestTemplate,可使用RestTemplateBuilder,新建類UserService.java

package com.example.restclient.service;import com.example.restclient.domain.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.web.client.RestTemplateBuilder;import org.springframework.context.annotation.Bean;import org.springframework.stereotype.Service;import org.springframework.web.client.RestTemplate;@Servicepublic class UserService { @Autowired private RestTemplateBuilder builder; @Bean public RestTemplate restTemplate(){ return builder.rootUri('http://localhost:8080').build(); } public User userBuilder(String name){ User u = restTemplate().getForObject('/user/' + name, User.class); return u; }}

4、編寫一個(gè)單元測試類,來測試上面的UserService的bean。

package com.example.restclient.service;import com.example.restclient.domain.User;import org.junit.Assert;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)public class UserServiceTest { @Autowired private UserService userService; @Test public void testUser(){ User u = userService.userBuilder('lc'); Assert.assertEquals('lc', u.getName()); }}

5、控制器類UserController.cs 中調(diào)用

配置在application.properties 配置端口和8080不一樣,如server.port = 9001

@Autowired private UserService userService; @RequestMapping(value='/user/{name}', produces = MediaType.APPLICATION_JSON_VALUE) public User user(@PathVariable String name) { User u = userService.userBuilder(name); return u; }

三、使用Feign調(diào)用服務(wù)

繼續(xù)在rest-client項(xiàng)目基礎(chǔ)上修改代碼。

1、pom.xml添加依賴

<dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-core</artifactId> <version>9.5.0</version> </dependency> <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-gson</artifactId> <version>9.5.0</version> </dependency>

2、新建接口UserClient.java

package com.example.restclient.service;import com.example.restclient.domain.User;import feign.Param;import feign.RequestLine;public interface UserClient { @RequestLine('GET /user/{name}') User getUser(@Param('name')String name);}

3、在控制器類UserController.java 中調(diào)用

decoder(new GsonDecoder()) 表示添加了解碼器的配置,GsonDecoder會將返回的JSON字符串轉(zhuǎn)換為接口方法返回的對象。相反的,encoder(new GsonEncoder())則是編碼器,將對象轉(zhuǎn)換為JSON字符串。

@RequestMapping(value='/user2/{name}', produces = MediaType.APPLICATION_JSON_VALUE) public User user2(@PathVariable String name) { UserClient service = Feign.builder().decoder(new GsonDecoder()) .target(UserClient.class, 'http://localhost:8080/'); User u = service.getUser(name); return u; }

4、優(yōu)化第3步代碼,并把請求地址放到配置文件中。

(1)application.properties 添加配置

復(fù)制代碼 代碼如下:application.client.url = http://localhost:8080

(2)新建配置類ClientConfig.java

package com.example.restclient.config;import com.example.restclient.service.UserClient;import feign.Feign;import feign.gson.GsonDecoder;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class ClientConfig { @Value('${application.client.url}') private String clientUrl; @Bean UserClient userClient(){ UserClient client = Feign.builder().decoder(new GsonDecoder()).target(UserClient.class, clientUrl); return client; }}

(3)控制器 UserController.java 中調(diào)用

@Autowired private UserClient userClient; @RequestMapping(value='/user3/{name}', produces = MediaType.APPLICATION_JSON_VALUE) public User user3(@PathVariable String name) { User u = userClient.getUser(name); return u; }

UserController.java最終內(nèi)容:

package com.example.restclient.web;import com.example.restclient.domain.User;import com.example.restclient.service.UserClient;import com.example.restclient.service.UserService;import feign.Feign;import feign.gson.GsonDecoder;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.MediaType;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class UserController { @Autowired private UserService userService; @Autowired private UserClient userClient; @RequestMapping(value='/user/{name}', produces = MediaType.APPLICATION_JSON_VALUE) public User user(@PathVariable String name) { User u = userService.userBuilder(name); return u; } @RequestMapping(value='/user2/{name}', produces = MediaType.APPLICATION_JSON_VALUE) public User user2(@PathVariable String name) { UserClient service = Feign.builder().decoder(new GsonDecoder()) .target(UserClient.class, 'http://localhost:8080/'); User u = service.getUser(name); return u; } @RequestMapping(value='/user3/{name}', produces = MediaType.APPLICATION_JSON_VALUE) public User user3(@PathVariable String name) { User u = userClient.getUser(name); return u; }}

項(xiàng)目結(jié)構(gòu)

Spring Boot2發(fā)布調(diào)用REST服務(wù)實(shí)現(xiàn)方法

先后訪問下面地址,可見到輸出正常結(jié)果

http://localhost:9001/user/lchttp://localhost:9001/user2/lc2http://localhost:9001/user3/lc3

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
成人午夜网址| 精品国产亚洲一区二区三区在线| 日本一二区不卡| 国内在线观看一区二区三区| 精品理论电影在线| 久久精品国产一区二区| 精品国产91| 在线精品视频在线观看高清| 97视频热人人精品免费| 国产一区二区三区网| 老司机精品视频网| 国产精品mv在线观看| 久久在线视频免费观看| 在线亚洲精品| 国产欧美欧美| 中文字幕免费一区二区| 国产精品视频一区二区三区综合| 成人看片网站| 国产suv精品一区二区四区视频| 久久男人av| 国产精品theporn| 久久99蜜桃| 日韩天堂在线| 国产综合亚洲精品一区二| 综合视频一区| 国产精品亚洲片在线播放| 中文字幕人成乱码在线观看| 色老板在线视频一区二区| 日韩av午夜在线观看| 久久影院午夜精品| 乱一区二区av| 婷婷精品在线观看| 91精品一区二区三区综合| 美女黄网久久| 欧洲av不卡| av资源新版天堂在线| 国产欧美日韩综合一区在线播放| 国产精品蜜芽在线观看| 99视频精品全部免费在线视频| 亚洲午夜视频| 亚洲精品在线观看91| 日韩午夜黄色| 久久精品免视看国产成人| 精品国产成人| 蜜臀av国产精品久久久久| 鲁大师成人一区二区三区| 久久精品资源| 在线日韩中文| 亚洲啊v在线免费视频| 亚洲高清影视| 亚洲美女91| 日本麻豆一区二区三区视频| 欧美成人综合| 中文字幕视频精品一区二区三区| 久久a爱视频| 久热re这里精品视频在线6| 精品中国亚洲| 日韩国产一区二| 亚洲小说欧美另类婷婷| 久久久免费人体| 国产精品视频一区二区三区四蜜臂 | 日本亚洲欧美天堂免费| 视频在线观看91| 中文在线中文资源| 模特精品在线| 国产日韩专区| 日韩精品高清不卡| 精品一区不卡| 久久不卡日韩美女| 亚洲伊人影院| 99国产一区| 中文另类视频| 岛国av在线网站| 精品五月天堂| 日本在线成人| 欧美日韩亚洲在线观看| 91久久久精品国产| 午夜电影一区| 精品久久精品| 亚洲一区免费| 中文字幕亚洲影视| 国产精品美女午夜爽爽| 亚洲精选91| 福利一区二区三区视频在线观看| 亚洲午夜黄色| 久久久久国产精品一区三寸| 国产精品二区不卡| 日韩1区2区日韩1区2区| 久久一二三区| 日韩国产专区| 日韩一区二区中文| 午夜一区在线| 欧美交a欧美精品喷水| 日韩欧美1区| 亚洲天堂av资源在线观看| 欧美一区=区三区| 国产成人1区| 国产精品亚洲一区二区在线观看| 亚洲国产影院| sm久久捆绑调教精品一区| 国产精选一区| 亚洲精品美女| 视频一区国产视频| av亚洲在线观看| 你懂的国产精品| 99在线观看免费视频精品观看| 久久久久九九精品影院| 久久一区二区三区电影| 韩日一区二区| 国产精品麻豆成人av电影艾秋| 中文字幕亚洲精品乱码| 99视频精品免费观看| 91精品一区国产高清在线gif| 精品淫伦v久久水蜜桃| 精品国产一区二区三区av片| 成人看片网站| 成人精品久久| 麻豆精品少妇| 亚洲精品电影| 亚洲精品看片| 免费观看不卡av| 亚洲乱码视频| 欧美中文字幕一区二区| 欧美午夜三级| 99国产精品自拍| 成人综合一区| 国产伦理久久久久久妇女| 久久福利毛片| 日韩不卡视频在线观看| 国产欧美一区二区三区精品观看| 青青久久av| 麻豆传媒一区二区三区| 免费在线观看一区二区三区| 国产欧洲在线| 美国三级日本三级久久99 | 香蕉久久久久久久av网站| 久久中文字幕一区二区三区| 亚洲欧洲美洲国产香蕉| 999久久久精品国产| 国产精品亚洲人成在99www| 日韩精品一区二区三区中文| 欧美特黄a级高清免费大片a级| 成人亚洲精品| 国产精品a级| 玖玖玖国产精品| 亚洲精品一二三区区别| 伊伊综合在线| 久久久久黄色| 国产精品视频一区二区三区 | 亚洲我射av| 欧美日韩在线二区| 久久网站免费观看| 9久re热视频在线精品| 日韩精品一级中文字幕精品视频免费观看| 久久婷婷久久| 欧产日产国产精品视频| 欧美日韩色图| 久久国产精品99国产| 亚洲深夜影院| 免费在线成人网| 欧美日韩a区| 精品一区不卡| 欧美综合另类| 免费人成黄页网站在线一区二区| 亚洲专区在线| 日韩激情av在线| 久久99偷拍| 国产理论在线| 日韩视频在线一区二区三区 | 石原莉奈在线亚洲二区| 在线亚洲欧美| 日韩一区精品| 久久久久久婷| 妖精视频成人观看www| 日韩av一级片| 亚洲成人精品| 日本伊人久久| 四虎8848精品成人免费网站| 久久久久国产精品一区二区| 在线亚洲欧美| 国产aⅴ精品一区二区四区| 精品一区毛片| 欧美国产极品| 免费精品视频最新在线| 国产一区丝袜| 婷婷五月色综合香五月| 亚洲精品成人图区| 四虎精品一区二区免费| 久久天堂av| 久久精品xxxxx| 日韩专区在线视频| 激情视频网站在线播放色| 色婷婷成人网| 亚洲一区资源| 国产日韩免费| 亚洲精一区二区三区| 久久久夜夜夜| 国产成年精品| 亚洲在线一区| 久久99伊人|