老熟女激烈的高潮_日韩一级黄色录像_亚洲1区2区3区视频_精品少妇一区二区三区在线播放_国产欧美日产久久_午夜福利精品导航凹凸

重慶分公司,新征程啟航

為企業(yè)提供網(wǎng)站建設(shè)、域名注冊、服務(wù)器等服務(wù)

如何實現(xiàn)SpringBoot2發(fā)布調(diào)用REST服務(wù)

這篇文章主要講解了如何實現(xiàn)Spring Boot2發(fā)布調(diào)用REST服務(wù),內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。

創(chuàng)新互聯(lián)建站是一家專業(yè)提供昔陽企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站建設(shè)、做網(wǎng)站H5場景定制、小程序制作等業(yè)務(wù)。10年已為昔陽眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進行中。

開發(fā)環(huán)境:IntelliJ IDEA 2019.2.2
Spring 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;

@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;
  }
}

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

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

訪問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;

@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、編寫一個單元測試類,來測試上面的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添加依賴


      io.github.openfeign
      feign-core
      9.5.0
    

    
      io.github.openfeign
      feign-gson
      9.5.0
    

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;

@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;
  }
}

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

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

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

http://localhost:9001/user/lc
http://localhost:9001/user2/lc2
http://localhost:9001/user3/lc3

看完上述內(nèi)容,是不是對如何實現(xiàn)Spring Boot2發(fā)布調(diào)用REST服務(wù)有進一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。


分享文章:如何實現(xiàn)SpringBoot2發(fā)布調(diào)用REST服務(wù)
網(wǎng)址分享:http://www.xueling.net.cn/article/jojhgp.html

其他資訊

在線咨詢
服務(wù)熱線
服務(wù)熱線:028-86922220
TOP
主站蜘蛛池模板: 无码av一区在线观看免费 | 亚洲一级在线看 | 免费的黄| 日本狂喷奶水在线播放212 | 一级毛片播放 | 99精品1区2区 | 国产在线啪 | 欧美国产亚洲精品久久久8v | 99国产精品视频免费观看一公开 | 潜行者40集连续剧免费观看 | 日韩夜精品精品免费观看 | 成人免费视频网站 | 99热精国产这里只有精品 | 91豆花视频 | 亚洲大码熟女在线观看 | 手机看片一级片 | 成人av在线网 | 粉嫩av一区二区三区四区在线观看 | 日本在线一二三区 | 亚洲第5页| 少妇被躁爽到高潮无码麻豆AV | 色综合伊人色综合网站中国 | 1000部拍拍拍18勿入免费视频 | 无码区a毛片免费视频 | 少妇被两个黑人3p喷水在线观看 | 欧美熟妇色xxxx | 一区在线免费视频 | 老少配老妇老熟女中文普通话 | 91精品区 | 午夜一级精品 | 国产精品久久久久久久乖乖 | 国产一区二区三区四区五区精品 | 爆乳jk美女脱内衣裸体网站 | 日本人在线观看 | 久久麻传媒亚洲av国产 | 色妞色视频一区二区三区四区 | 久草网站 | 天天操天天艹 | 99热精国产这里只有精品 | 欧美嫩交一区二区三区 | 欧美日韩亚洲成人 |