国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

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

瀏覽:148日期: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)文章:
主站蜘蛛池模板: 国产三级理论 | 亚洲国产精品久久日 | 美女张腿男人桶免费视频 | 精品精品国产欧美在线观看 | 午夜视频在线观看一区 | 免费狼人久久香蕉网 | 一区二区三区网站在线免费线观看 | 日韩一级a毛片欧美区 | 久久国产精品女 | 国产美女精品一区二区三区 | 在线午夜影院 | 艳女伦交一级毛片 | 久久综合香蕉久久久久久久 | 国产99视频精品免费视频免里 | 午夜香蕉成视频人网站高清版 | 国产激情视频在线 | 狠狠综合久久久久综合小说网 | 大陆老头xxxxxhd | 欧美一级成人影院免费的 | 欧美xxxxx九色视频免费观看 | 国产免费久久精品99 | 日韩毛片大全免费高清 | 9久久免费国产精品特黄 | 米奇777色狠狠8888影视 | 亚洲成a人片在线观看中 | 私人毛片免费高清影视院丶 | 三级视频网站在线观看 | 精品毛片免费看 | 久久精品3 | 亚洲精品专区一区二区欧美 | 欧美一区二区二区 | 亚洲最黄视频 | 久草手机视频在线 | 欧美成人免费全网站大片 | 538prom精品视频在放免费 | 中文字幕一区二区三区久久网站 | 99热久久精品免费精品 | 国产在线极品 | 亚洲精品一区二区三区国产 | 日本在线免费播放 | 奇米5555|