其他分享
首页 > 其他分享> > RestTemplate简单使用

RestTemplate简单使用

作者:互联网

RestTemplate来自import org.springframework.web.client.RestTemplate;
需要的maven依赖
<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

  

 

 

get无参调用

接口项目代码

<?xml version="1.0" encoding="UTF-8"?>
<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>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.7.1</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com</groupId>
	<artifactId>rest</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>rest</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

  

package com.rest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class RestApplication {

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

}

/**
 * 
 */
package com.rest.controller.front;

import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yourheart
 * @update 2022年6月28日 下午11:08:02
 * @description:
 */
@RestController
public class TestController {

    @GetMapping("/testNoParamterGet")
    public Map<String, Object> testNoParamterGet() {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("name", "小白");
        resultMap.put("age", "10");
        resultMap.put("hobby", "羽毛球");
        resultMap.put("address", "东晋");
        return resultMap;
    }

}


server.port=8001


logging.level.com.rest=debug
logging.level.web=debug
spring.devtools.add-properties=false

  调用方代码

<?xml version="1.0" encoding="UTF-8"?>
<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>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.7.0</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.java</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>


	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

  

package com.java;

/**
 * @author yourheart
 * @Description
 * @create 2022-06-20 21:14
 */
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.web.client.RestTemplate;


@SpringBootTest
public class DemoApplicationTests {



    @Test
    public void test(){

        RestTemplate restTemplate=new RestTemplate();
        String url="http://127.0.0.1:8001//testNoParamterGet";
        String forObject = restTemplate.getForObject(url, String.class);
        System.out.println(forObject);


    }
}

  

server.port=2000

logging.level.com.java=debug
logging.level.web=debug
spring.devtools.add-properties=false

  get有参调用

 

 

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

@GetMapping("/testParamterGet")
    public Map<String, Object> testParamterGet(String param) {
        log.info("【入参】param:{}", param);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("name", "小红");
        resultMap.put("age", "20");
        resultMap.put("hobby", "网球");
        resultMap.put("address", "唐朝");
        return resultMap;
    }

  

 

package com.java;

/**
 * @author yourheart
 * @Description
 * @create 2022-06-20 21:14
 */
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.web.client.RestTemplate;


@SpringBootTest
public class DemoApplicationTests {



    @Test
    public void test(){

        RestTemplate restTemplate=new RestTemplate();
        String url="http://127.0.0.1:8001//testParamterGet?param={param}";
        String forObject = restTemplate.getForObject(url, String.class,"aaa");
        System.out.println(forObject);


    }
}

  get请求多个参数

@GetMapping("/testParamterMapGet/{id}/{name}")
    public Map<String, Object> testParamterMapGet(@PathVariable String id, @PathVariable String name) {
        log.info("【入参】id:{},name:{}", id, name);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("name", "小红");
        resultMap.put("age", "20");
        resultMap.put("hobby", "网球");
        resultMap.put("address", "唐朝");
        return resultMap;
    }

  

package com.java;

/**
 * @author yourheart
 * @Description
 * @create 2022-06-20 21:14
 */
import com.java.bean.TestBean;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;


@SpringBootTest
public class DemoApplicationTests {



    @Test
    public void test(){

        RestTemplate restTemplate=new RestTemplate();
        String url="http://127.0.0.1:8001//testParamterMapGet/{id}/{name}";
        Map<String, String> params = new HashMap<String, String>();
        params.put("id", "1");
        params.put("name","admin");
        String forObject = restTemplate.getForObject(url, String.class,params);
        System.out.println(forObject);


    }
}

 

 

 

post请求多个参数

@PostMapping("/testParamterBeanPost")
    public Map<String, Object> testParamterBeanPost(@RequestBody TestBean param) {
        log.info("【入参】param:{}", param);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("name", "小红");
        resultMap.put("age", "20");
        resultMap.put("hobby", "网球");
        resultMap.put("address", "唐朝");
        return resultMap;
    }

  

/**
 * 
 */
package com.rest.bean;

/**
 * @author yourheart
 * @update 2022年6月28日 下午11:56:52
 * @description:
 */
public class TestBean {

    private String user;

    private String pwd;

    /**
     * @return the user
     */
    public String getUser() {
        return user;
    }

    /**
     * @param user the user to set
     */
    public void setUser(String user) {
        this.user = user;
    }

    /**
     * @return the pwd
     */
    public String getPwd() {
        return pwd;
    }

    /**
     * @param pwd the pwd to set
     */
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    @Override
    public String toString() {
        return "TestBean [user=" + user + ", pwd=" + pwd + "]";
    }

}

  

package com.java;

/**
 * @author yourheart
 * @Description
 * @create 2022-06-20 21:14
 */

import com.java.bean.TestBean;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;


@SpringBootTest
public class DemoApplicationTests {


    @Test
    public void test() {

        RestTemplate restTemplate = new RestTemplate();
        String url = "http://127.0.0.1:8001/testParamterBeanPost";
        TestBean testBean = new TestBean();
        testBean.setUser("admin");
        testBean.setPwd("123");
        String postForObject = restTemplate.postForObject(url, testBean, String.class);
        System.out.println(postForObject);
    }
}

  

/**
 * 
 */
package com.java.bean;

/**
 * @author yourheart
 * @update 2022年6月28日 下午11:56:52
 * @description:
 */
public class TestBean {

    private String user;

    private String pwd;

    /**
     * @return the user
     */
    public String getUser() {
        return user;
    }

    /**
     * @param user the user to set
     */
    public void setUser(String user) {
        this.user = user;
    }

    /**
     * @return the pwd
     */
    public String getPwd() {
        return pwd;
    }

    /**
     * @param pwd the pwd to set
     */
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    @Override
    public String toString() {
        return "TestBean [user=" + user + ", pwd=" + pwd + "]";
    }

}

  

 

 

 

标签:String,boot,简单,RestTemplate,resultMap,使用,org,import,public
来源: https://www.cnblogs.com/q202105271618/p/16420280.html