Spring Rest Client示例

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

 

posted @ 2022-07-27 23:16  sunnyamy  阅读(76)  评论(0编辑  收藏  举报