SpringGuide(half 原创):如何使用一个REST服务?

26 篇文章 0 订阅

在SpringGuide官方中是有一个文章叫做:Consuming a RESTful Web Service。在这个文章中,是访问了一个REST服务: https://gturnquist-quoters.cfapps.io/api/random    这个服务是返回id 和一段关于Spirng的引文,这个REST服务是现成的,也是你掌控不了的。同时在官方Guide中还有一个文章:Building a RESTful Web Service 。在这个文章中啊,构建了一个REST服务,构建完成之后,浏览器中输入http://localhost:8080/greeting返回:输入http://localhost:8080/greeting?name=Lan返回:

重点来了,本文的目的就是要达到用程序代码的方式来使用这个自己创建的REST服务。注意:这个REST服务是运行在8080 端口的,你的使用REST程序的端口是运行在9090.

程序的主体还是这个 Consuming a RESTful Web Service  Guide中 的 ,给这个下面添加一个类:Greeting ,类的代码可以和《Building a RESTful Web Service》中的一样,

package com.example.restservice;

public class Greeting {

	private final long id;
	private final String content;

	public Greeting(long id, String content) {
		this.id = id;
		this.content = content;
	}

	public long getId() {
		return id;
	}

	public String getContent() {
		return content;
	}
}

 

把这个类创建黏贴后,需要修改一下包名,以及添加一个toString方法最后代码如下:

package com.example.consumingrest;

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

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

 

然后就可以在主程序类中调用这个REST了。

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://gturnquist-quoters.cfapps.io/api/random", Quote.class);
            log.info(quote.toString());
            Quote quote1 = restTemplate.getForObject("https://gturnquist-quoters.cfapps.io/api/random", Quote.class);
            log.info(quote1.toString());
            Quote quote2 = restTemplate.getForObject("https://gturnquist-quoters.cfapps.io/api/random", Quote.class);
            log.info(quote2.toString());
        
            Greeting gt1=restTemplate.getForObject("http://localhost:8080/greeting",Greeting.class);
            log.info(gt1.toString());
            Greeting gt2=restTemplate.getForObject("http://localhost:8080/greeting?name=King",Greeting.class);
            log.info(gt2.toString());
        };
    }
}

 gt1是发起的url是http://localhost:8080/greeting 默认的,没有参数

gt2是url是http://localhost:8080/greeting?name=King 是名字叫king ,带有名字参数。

运行结果可以在IDE的控制台看到:

想用英雄Lan 就这个修改ConsumingRestApplication类,添加下列代码:

Greeting gt3=restTemplate.getForObject("http://localhost:8080/greeting?name=Lan",Greeting.class);
            log.info(gt2.toString());

即可。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值