【练习】2.Consuming a RESTful Web Service

Starting with Spring Initializr

Fetching a REST Resource

First, you need to create a domain class to contain the data that you need. The following listing shows the Quote class, which you can use as your domain class:

src/main/java/com/example/consumingrest/Quote.java

copy

package com.example.consumingrest;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Quote {

  private String type;
  private Value value;

  public Quote() {
  }

  public String getType() {
    return type;

You also need an additional class, to embed the inner quotation itself. The Value class fills that need and is shown in the following listing (at src/main/java/com/example/consumingrest/Value.java)

copy

package com.example.consumingrest;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Value {

  private Long id;
  private String quote;

  public Value() {
  }

  public Long getId() {
    return this.id;
  }

  public String getQuote() {
    return this.quote;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public void setQuote(String quote) {
    this.quote = quote;
  }

  @Override
  public String toString() {
    return "Value{" +
        "id=" + id +
        ", quote='" + quote + '\'' +
        '}';
  }
}

Finishing the Application

Now you need to add a few other things to the ConsumingRestApplication class to get it to show quotations from our RESTful source. You need to add:

  • A logger, to send output to the log (the console, in this example).

  • RestTemplate, which uses the Jackson JSON processing library to process the incoming data.

  • CommandLineRunner that runs the RestTemplate (and, consequently, fetches our quotation) on startup.

The following listing shows the finished ConsumingRestApplication class (at src/main/java/com/example/consumingrest/ConsumingRestApplication.java

package com.example.consumingrest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class ConsumingRestApplication {

    private static final Logger log = LoggerFactory.getLogger(ConsumingRestApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(ConsumingRestApplication.class, args);
    }

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }

    @Bean
    public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
        return args -> {
            Quote quote = restTemplate.getForObject(
                    "https://quoters.apps.pcfone.io/api/random", Quote.class);
            log.info(quote.toString());
        };
    }
}

run 

mvnw spring-boot:run

  public void setType(String type) {
    this.type = type;
  }

  public Value getValue() {
    return value;
  }

  public void setValue(Value value) {
    this.value = value;
  }

  @Override
  public String toString() {
    return "Quote{" +
        "type='" + type + '\'' +
        ", value=" + value +
        '}';
  }
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
As an AI language model, I do not have access to the specific context of the issue you are facing. However, here are some general steps that you can take to troubleshoot the issue: 1. Check the logs: The first step in troubleshooting any service-related issue is to check the logs. In most cases, you can find the logs for Redis service in the /var/log/redis/redis-server.log file. Check the logs for any error messages or warnings that might indicate the cause of the failure. 2. Check the configuration: Make sure that the Redis configuration file (/etc/redis/redis.conf) is properly configured. Check the file for any syntax errors, typos, or incorrect settings. 3. Restart the service: Try restarting the Redis service using the following command: sudo systemctl restart redis 4. Check the status: After restarting the service, check the status of the Redis service to see if it is running: sudo systemctl status redis If the status shows that the service is still failing, check the logs again for any new error messages. 5. Check the system resources: If Redis is consuming too much system resources, it may fail to start or crash. Check the system resources (CPU, memory, disk space) and make sure that they are not being exhausted. 6. Reinstall Redis: If none of the above steps work, you may need to reinstall Redis. First, remove the existing Redis installation using the following command: sudo apt-get remove redis-server Then, reinstall Redis using the following command: sudo apt-get install redis-server Note that this will remove all existing Redis data, so you will need to backup and restore any Redis data that you need.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值