电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

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

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

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

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

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

1、IDEA新建一個名稱為rest-server的Spring Boot項目

2、新建一個實體類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、新建一個控制器類 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; }}

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

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

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

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

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

1、IDEA新建一個名稱為rest-client的Spring Boot項目

2、新建一個含有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、編寫一個單元測試類,來測試上面的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項目基礎(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; }}

項目結(jié)構(gòu)

Spring Boot2發(fā)布調(diào)用REST服務(wù)實現(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)文章:
主站蜘蛛池模板: 欧美日韩国产一区二区三区不_久久久久国产精品无码不卡_亚洲欧洲美洲无码精品AV_精品一区美女视频_日韩黄色性爱一级视频_日本五十路人妻斩_国产99视频免费精品是看4_亚洲中文字幕无码一二三四区_国产小萍萍挤奶喷奶水_亚洲另类精品无码在线一区 | 全温恒温摇床-水浴气浴恒温摇床-光照恒温培养摇床-常州金坛精达仪器制造有限公司 | 二氧化碳/活性炭投加系统,次氯酸钠发生器,紫外线消毒设备|广州新奥 | 不锈钢列管式冷凝器,换热器厂家-无锡飞尔诺环境工程有限公司 | 太阳能发电系统-太阳能逆变器,控制器-河北沐天太阳能科技首页 | 测试治具|过炉治具|过锡炉治具|工装夹具|测试夹具|允睿自动化设备 | 臭氧发生器_臭氧消毒机 - 【同林品牌 实力厂家】 | 颗粒机,颗粒机组,木屑颗粒机-济南劲能机械有限公司 | 股指期货-期货开户-交易手续费佣金加1分-保证金低-期货公司排名靠前-万利信息开户 | 滚塑PE壳体-PE塑料浮球-警示PE浮筒-宁波君益塑业有限公司 | 单锥双螺旋混合机_双螺旋锥形混合机-无锡新洋设备科技有限公司 | 单锥双螺旋混合机_双螺旋锥形混合机-无锡新洋设备科技有限公司 | 定做大型恒温循环水浴槽-工业用不锈钢恒温水箱-大容量低温恒温水槽-常州精达仪器 | 深圳希玛林顺潮眼科医院(官网)│深圳眼科医院│医保定点│香港希玛林顺潮眼科中心连锁品牌 | 河南mpp电力管_mpp电力管生产厂家_mpp电力电缆保护管价格 - 河南晨翀实业 | 手术室净化厂家-成都做医院净化工程的公司-四川华锐-15年特殊科室建设经验 | 杭州公司变更法人-代理记账收费价格-公司注销代办_杭州福道财务管理咨询有限公司 | 高压分散机(高压细胞破碎仪)百科-北京天恩瀚拓 | 茶叶百科网-茶叶知识与茶文化探讨分享平台 | 贝朗斯动力商城(BRCPOWER.COM) - 买叉车蓄电池上贝朗斯商城,价格更超值,品质有保障! | 安平县鑫川金属丝网制品有限公司,防风抑尘网,单峰防风抑尘,不锈钢防风抑尘网,铝板防风抑尘网,镀铝锌防风抑尘网 | 北京办公室装修,办公室设计,写字楼装修-北京金视觉装饰工程公司 北京成考网-北京成人高考网 | 超声波分散机-均质机-萃取仪-超声波涂料分散设备-杭州精浩 | 阻垢剂,反渗透阻垢剂,缓蚀阻垢剂-山东普尼奥水处理科技有限公司 真空粉体取样阀,电动楔式闸阀,电动针型阀-耐苛尔(上海)自动化仪表有限公司 | 净化板-洁净板-净化板价格-净化板生产厂家-山东鸿星新材料科技股份有限公司 | 二手电脑回收_二手打印机回收_二手复印机回_硒鼓墨盒回收-广州益美二手电脑回收公司 | 固诺家居-全屋定制十大品牌_整体衣柜木门橱柜招商加盟 | 【甲方装饰】合肥工装公司-合肥装修设计公司,专业从事安徽办公室、店面、售楼部、餐饮店、厂房装修设计服务 | 耐酸泵,耐酸泵厂家-淄博华舜耐腐蚀真空泵 | 北京网站建设首页,做网站选【优站网】,专注北京网站建设,北京网站推广,天津网站建设,天津网站推广,小程序,手机APP的开发。 | 广州监控安装公司_远程监控_安防弱电工程_无线wifi覆盖_泉威安防科技 | 直读光谱仪,光谱分析仪,手持式光谱仪,碳硫分析仪,创想仪器官网 | 湖南长沙商标注册专利申请,长沙公司注册代理记账首选美创! | 有机肥设备生产制造厂家,BB掺混肥搅拌机、复合肥设备生产线,有机肥料全部加工设备多少钱,对辊挤压造粒机,有机肥造粒设备 -- 郑州程翔重工机械有限公司 | PCB厂|线路板厂|深圳线路板厂|软硬结合板厂|电路板生产厂家|线路板|深圳电路板厂家|铝基板厂家|深联电路-专业生产PCB研发制造 | 冰晶石|碱性嫩黄闪蒸干燥机-有机垃圾烘干设备-草酸钙盘式干燥机-常州市宝康干燥 | 油缸定制-液压油缸厂家-无锡大鸿液压气动成套有限公司 | 塑胶地板-商用PVC地板-pvc地板革-安耐宝pvc塑胶地板厂家 | 废旧物资回收公司_广州废旧设备回收_报废设备物资回收-益美工厂设备回收公司 | 西安标准厂房_陕西工业厂房_西咸新区独栋厂房_长信科技产业园官方网站 | 安徽净化板_合肥岩棉板厂家_玻镁板厂家_安徽科艺美洁净科技有限公司 |