ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Spring Rest Client示例

2022-07-27 23:37:50  阅读:149  来源: 互联网

标签:restclient 96314 示例 Spring springframework RestExamples Client org import


RestExamples.java

package tacos.restclient;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.client.RestTemplate;

import lombok.extern.slf4j.Slf4j;

@SpringBootConfiguration
@ComponentScan
@Slf4j
public class RestExamples {
    public static void main(String[] args) {
        SpringApplication.run(RestExamples.class, args);
    }
    
    @Bean
    public RestTemplate restTemplate() {
      return new RestTemplate();
    }
    
    @Bean
    public CommandLineRunner fetchAlbum(XmlyClient xmlyClient) {
      return args -> {
        log.info("----------------------- GET -------------------------");
        log.info("GETTING album BY id");
        log.info("Album:  " + xmlyClient.getAlbumInfo(0));
      };
    }
}
View Code

 

XmlyClient.java

package tacos.restclient;

import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import lombok.extern.slf4j.Slf4j;
import tacos.Album;

@Service
@Slf4j
public class XmlyClient {
  private RestTemplate rest;

  public XmlyClient(RestTemplate rest) {
    this.rest = rest;
  }
  
  public Object getAlbumInfo(Integer albumId) {
      return rest.getForObject("https://www.ximalaya.com/tdk-web/seo/search/albumInfo?albumId=3561241", Object.class);
  }
}
View Code

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>sia</groupId>
    <artifactId>taco-cloud-ch6-2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>tacocloud2-restclient</artifactId>
  <name>tacocloud2-restclient</name>
  <description>tacocloud2-restclient</description>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
  </properties>
  
  <dependencies>
    <!-- Necessary Boot starters -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <!-- TacoCloud dependencies -->
    <dependency>
      <groupId>sia</groupId>
      <artifactId>tacocloud2-domain</artifactId>
      <version>${tacocloud.version}</version>
    </dependency>
  </dependencies>
  
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>
View Code

 

运行结果如下:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.7.RELEASE)

2022-07-27 23:15:24.323  INFO 96314 --- [  restartedMain] tacos.restclient.RestExamples            : Starting RestExamples on sunlihuadeMacBook-Pro.local with PID 96314 (/Users/sunlihua/Documents/workspace-spring-tool-suite-4/taco-cloud-ch6-2/tacocloud2-restclient/target/classes started by sunlihua in /Users/sunlihua/Documents/workspace-spring-tool-suite-4/taco-cloud-ch6-2/tacocloud2-restclient)
2022-07-27 23:15:24.325  INFO 96314 --- [  restartedMain] tacos.restclient.RestExamples            : No active profile set, falling back to default profiles: default
2022-07-27 23:15:24.344  INFO 96314 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2022-07-27 23:15:24.344  INFO 96314 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2022-07-27 23:15:24.500  INFO 96314 --- [  restartedMain] tacos.restclient.RestExamples            : Started RestExamples in 0.506 seconds (JVM running for 0.809)
2022-07-27 23:15:24.501  INFO 96314 --- [  restartedMain] tacos.restclient.RestExamples            : ----------------------- GET -------------------------
2022-07-27 23:15:24.501  INFO 96314 --- [  restartedMain] tacos.restclient.RestExamples            : GETTING album BY id
2022-07-27 23:15:24.757  INFO 96314 --- [  restartedMain] tacos.restclient.RestExamples            : Album:  {ret=200, msg=成功, data={albumId=3561241, albumTitle=六级词汇词根+联想记忆法, albumIntro=《六级词汇词根+联想记忆法》是俞敏洪老师编著的经典“词根+联想”系列词汇书之一,已陪伴数百万名考生走过备战六级的岁月,广受读者欢迎。本书囊括六级新大纲词表所有新增词,删除新大纲词表中已经删除的词汇,紧跟新大纲改革趋势,匹配新版考试大纲词表..., lastUptrackId=445566339, trackTitle=熟词僻义表, trackIntro=, trackCount=32}}
View Code

 

标签:restclient,96314,示例,Spring,springframework,RestExamples,Client,org,import
来源: https://www.cnblogs.com/greatai/p/16526945.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有