Spring Boot2發(fā)布調(diào)用REST服務(wù)實(shí)現(xiàn)方法
開(kāi)發(fā)環(huán)境:IntelliJ IDEA 2019.2.2
Spring Boot版本:2.1.8
一、發(fā)布REST服務(wù)
1、IDEA新建一個(gè)名稱(chēng)為rest-server的Spring Boot項(xiàng)目
2、新建一個(gè)實(shí)體類(lèi)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è)控制器類(lèi) 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; @RestController public 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)如下:
訪問(wèn)http://localhost:8080/user/lc,頁(yè)面顯示:
{"name":"lc","age":30}
二、使用RestTemplae調(diào)用服務(wù)
1、IDEA新建一個(gè)名稱(chēng)為rest-client的Spring Boot項(xiàng)目
2、新建一個(gè)含有main方法的普通類(lèi)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()',控制臺(tái)輸出:lc,30
3、在bean里面使用RestTemplate,可使用RestTemplateBuilder,新建類(lèi)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; @Service public 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、編寫(xiě)一個(gè)單元測(cè)試類(lèi),來(lái)測(cè)試上面的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、控制器類(lèi)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添加依賴(lài)
<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、在控制器類(lèi)UserController.java 中調(diào)用
decoder(new GsonDecoder()) 表示添加了解碼器的配置,GsonDecoder會(huì)將返回的JSON字符串轉(zhuǎn)換為接口方法返回的對(duì)象。
相反的,encoder(new GsonEncoder())則是編碼器,將對(duì)象轉(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步代碼,并把請(qǐng)求地址放到配置文件中。
(1)application.properties 添加配置
(2)新建配置類(lèi)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; @Configuration public 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; @RestController public 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)
先后訪問(wèn)下面地址,可見(jiàn)到輸出正常結(jié)果
http://localhost:9001/user/lc
http://localhost:9001/user2/lc2
http://localhost:9001/user3/lc3
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot http請(qǐng)求注解@RestController原理解析
- Spring boot2X Consul如何通過(guò)RestTemplate實(shí)現(xiàn)服務(wù)調(diào)用
- SpringBoot Security整合JWT授權(quán)RestAPI的實(shí)現(xiàn)
- 詳解SpringBoot中RestTemplate的幾種實(shí)現(xiàn)
- SpringBoot框架RESTful接口設(shè)置跨域允許
- Springboot RestTemplate 簡(jiǎn)單使用解析
- SpringBoot+Spring Security+JWT實(shí)現(xiàn)RESTful Api權(quán)限控制的方法
- Spring Boot RestTemplate提交表單數(shù)據(jù)的三種方法
相關(guān)文章
Spring Security OAuth 個(gè)性化token的使用
這篇文章主要介紹了Spring Security OAuth 個(gè)性化token的使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02Java Socket實(shí)現(xiàn)聊天室附1500行源代碼
Socket是應(yīng)用層與TCP/IP協(xié)議族通信的中間軟件抽象層,它是一組接口。本篇文章手把手帶你通過(guò)Java Socket來(lái)實(shí)現(xiàn)自己的聊天室,大家可以在過(guò)程中查缺補(bǔ)漏,溫故而知新2021-10-10一文搞懂Runnable、Callable、Future、FutureTask及應(yīng)用
一般創(chuàng)建線程只有兩種方式,一種是繼承Thread,一種是實(shí)現(xiàn)Runnable接口,在Java1.5之后就有了Callable、Future,這二種可以提供線程執(zhí)行完的結(jié)果,本文主要介紹了Runnable、Callable、Future、FutureTask及應(yīng)用,感興趣的可以了解一下2023-08-08提高開(kāi)發(fā)效率Live?Templates使用技巧詳解
這篇文章主要為大家介紹了提高開(kāi)發(fā)效率Live?Templates使用技巧詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Java集合Map常見(jiàn)問(wèn)題_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)整理了Java集合Map常見(jiàn)問(wèn)題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05JAVA8 List<List<Integer>> list中再裝一個(gè)list轉(zhuǎn)成一個(gè)list操
這篇文章主要介紹了JAVA8 List<List<Integer>> list中再裝一個(gè)list轉(zhuǎn)成一個(gè)list操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08java中staticclass靜態(tài)類(lèi)詳解
這篇文章主要介紹了java中staticclass靜態(tài)類(lèi)詳解,具有一定借鑒價(jià)值,需要的朋友可以了解下。2017-12-12IDEA?2022?中的Lombok?使用基礎(chǔ)教程
? Lombok是使用java編寫(xiě)的一款開(kāi)源類(lèi)庫(kù)。其主作用是使用注解來(lái)代替一些具有格式固定,沒(méi)有過(guò)多技術(shù)含量的編碼工作,這篇文章主要介紹了IDEA?2022?中的Lombok?使用基礎(chǔ)教程,需要的朋友可以參考下2022-12-12Java解析Excel文件并把數(shù)據(jù)存入數(shù)據(jù)庫(kù)
本篇文章主要介紹了Java解析Excel文件并把數(shù)據(jù)存入數(shù)據(jù)庫(kù) ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05