spring boot rest template——迹忆客-ag捕鱼王app官网

rest template 用于创建使用 restful web 服务的应用程序。 我们可以使用 exchange() 方法为所有 http 方法使用 web 服务。下面给出的代码显示了如何为 rest template 创建 bean 以自动连接 rest template 对象。

@springbootapplication
public class myapplication {
    public static void main(string[] args) {
        springapplication.run(myapplication.class, args);
    }
    @bean
    public resttemplate getresttemplate() {
        return new resttemplate();
    }
}

get

使用 resttemplate - exchange() 方法使用 get api

假设此 url http://localhost:8080/products 返回以下 json,我们将使用以下代码通过 rest template 使用此 api 响应

[
   {
      "id": "1",
      "name": "honey"
   },
   {
      "id": "2",
      "name": "almond"
   }
]

我们必须按照给定的要点来使用 api

  • 自动装配 rest 模板对象。
  • 使用 httpheaders 设置请求标头。
  • 使用 httpentity 封装请求对象。
  • 为 exchange() 方法提供 url、httpmethod 和返回类型。
@restcontroller
public class consumewebservice {
   @autowired
   resttemplate resttemplate;
   @requestmapping(value = "/template/products")
   public string getproductlist() {
      httpheaders headers = new httpheaders();
      headers.setaccept(arrays.aslist(mediatype.application_json));
      httpentity  entity = new httpentity(headers);
      
      return resttemplate.exchange("
         http://localhost:8080/products", httpmethod.get, entity, string.class).getbody();
   }
}

post

使用 resttemplate - exchange() 方法使用 post api

假设这个 url http://localhost:8080/products 返回如下所示的响应,我们将使用 rest template 来使用这个 api 响应。

下面给出的代码是请求正文

{
   "id":"3",
   "name":"迹忆客"
}

下面给出的代码是响应体

商品创建成功

我们必须按照给定的要点来使用 api

  • 自动装配 rest 模板对象。
  • 使用 httpheaders 设置请求标头。
  • 使用 httpentity 来封装请求对象。 在这里,我们包装 product 对象以将其发送到请求正文。
  • 为 exchange() 方法提供 url、httpmethod 和返回类型。
@restcontroller
public class consumewebservice {
   @autowired
   resttemplate resttemplate;
   @requestmapping(value = "/template/products", method = requestmethod.post)
   public string createproducts(@requestbody product product) {
      httpheaders headers = new httpheaders();
      headers.setaccept(arrays.aslist(mediatype.application_json));
      httpentity entity = new httpentity(product,headers);
      
      return resttemplate.exchange(
         "http://localhost:8080/products", httpmethod.post, entity, string.class).getbody();
   }
}

put

使用 resttemplate - exchange() 方法使用 put api

假设此 url http://localhost:8080/products/3 返回以下响应,我们将使用 rest template 使用此 api 响应。

下面给出的代码是请求正文

{
   "name":"迹忆客题库"
}

下面给出的代码是响应体

商品更新成功

我们必须按照给定的要点来使用 api

  • 自动装配 rest 模板对象。
  • 使用 httpheaders 设置请求标头。
  • 使用 httpentity 来封装请求对象。 在这里,我们包装 product 对象以将其发送到请求正文。
  • 为 exchange() 方法提供 url、httpmethod 和返回类型。
@restcontroller
public class consumewebservice {
   @autowired
   resttemplate resttemplate;
   @requestmapping(value = "/template/products/{id}", method = requestmethod.put)
   public string updateproduct(@pathvariable("id") string id, @requestbody product product) {
      httpheaders headers = new httpheaders();
      headers.setaccept(arrays.aslist(mediatype.application_json));
      httpentity entity = new httpentity(product,headers);
      
      return resttemplate.exchange(
         "http://localhost:8080/products/" id, httpmethod.put, entity, string.class).getbody();
   }
}

delete

使用 resttemplate - exchange() 方法使用 delete api

假设这个 url http://localhost:8080/products/3 返回下面给出的响应,我们将使用 rest template 使用这个 api 响应。

下面显示的这行代码就是响应体

商品删除成功

我们必须遵循以下几点来使用 api

  • 自动装配 rest 模板对象。
  • 使用 httpheaders 设置请求标头。
  • 使用 httpentity 封装请求对象。
  • 为 exchange() 方法提供 url、httpmethod 和返回类型。
@restcontroller
public class consumewebservice {
   @autowired
   resttemplate resttemplate;
   @requestmapping(value = "/template/products/{id}", method = requestmethod.delete)
   public string deleteproduct(@pathvariable("id") string id) {
      httpheaders headers = new httpheaders();
      headers.setaccept(arrays.aslist(mediatype.application_json));
      httpentity entity = new httpentity(headers);
      
      return resttemplate.exchange(
         "http://localhost:8080/products/" id, httpmethod.delete, entity, string.class).getbody();
   }
}

下面给出了完整的 rest template controller 类文件

package com.study.controller;
import com.study.model.product;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.http.httpentity;
import org.springframework.http.httpheaders;
import org.springframework.http.httpmethod;
import org.springframework.http.mediatype;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.resttemplate;
import java.util.arrays;
/**
 * @author jiyik.com
 */
@restcontroller
public class consumewebservice {
    @autowired
    resttemplate resttemplate;
    @requestmapping(value = "/template/products")
    public string getproductlist() {
        httpheaders headers = new httpheaders();
        headers.setaccept(arrays.aslist(mediatype.application_json));
        httpentity entity = new httpentity(headers);
        return resttemplate.exchange("http://localhost:8080/products", httpmethod.get, entity, string.class).getbody();
    }
    @requestmapping(value = "/template/products", method = requestmethod.post)
    public string createproducts(@requestbody product product) {
        httpheaders headers = new httpheaders();
        headers.setaccept(arrays.aslist(mediatype.application_json));
        httpentity entity = new httpentity(product, headers);
        return resttemplate.exchange("http://localhost:8080/products", httpmethod.post, entity, string.class).getbody();
    }
    @requestmapping(value = "/template/products/{id}", method = requestmethod.put)
    public string updateproduct(@pathvariable("id") string id, @requestbody product product) {
        httpheaders headers = new httpheaders();
        headers.setaccept(arrays.aslist(mediatype.application_json));
        httpentity entity = new httpentity(product, headers);
        return resttemplate.exchange("http://localhost:8080/products" id, httpmethod.put, entity, string.class).getbody();
    }
    @requestmapping(value = "/template/products/{id}", method = requestmethod.delete)
    public string deleteproduct(@pathvariable("id") string id) {
        httpheaders headers = new httpheaders();
        headers.setaccept(arrays.aslist(mediatype.application_json));
        httpentity entity = new httpentity(headers);
        return resttemplate.exchange("http://localhost:8080/products" id, httpmethod.delete, entity, string.class).getbody();
    }
}

spring boot 应用程序类 - myapplication.java 的代码如下所示

package com.study;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.context.annotation.bean;
import org.springframework.web.client.resttemplate;
/**
 * @author jiyik.com
 */
@springbootapplication
public class myapplication {
    public static void main(string[] args) {
        springapplication.run(myapplication.class, args);
    }
    @bean
    public resttemplate getresttemplate() {
        return new resttemplate();
    }
}

maven 构建的代码 – pom.xml 如下所示



    4.0.0
    com.spring
    springbootproject
    1.0-snapshot
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.6
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

gradle build - build.gradle 的代码如下

buildscript {
   ext {
      springbootversion = '1.5.8.release'
   }
   repositories {
      mavencentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springbootversion}")
   }
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
group = 'com.study'
version = '0.0.1-snapshot'
sourcecompatibility = 1.8
repositories {
   mavencentral()
}
dependencies {
   compile('org.springframework.boot:spring-boot-starter-web')
   testcompile('org.springframework.boot:spring-boot-starter-test')
}

这里我们使用 idea 来启动服务(读者也可以使用 mvn clean install 或者 gradle clean build 生成可执行jar包)

spring boot 异常处理 idea启动服务

现在在 apifox 应用程序中点击下面显示的 url 并查看输出。

get api 获取商品 url : http://localhost:8080/products

apifox 请求spring boot 获取商品

post api 创建商品 url : http://localhost:8080/products

apifox 请求spring boot 创建商品

put api 修改商品 url : http://localhost:8080/products/3

apifox 请求 spring boot 修改商品

delete api 删除商品 url : http://localhost:8080/products/3

apifox 请求 spring boot 删除商品

查看笔记

扫码一下
查看教程更方便
网站地图