Skip to content

Commit

Permalink
feat(quick-rest-template): resttemplate
Browse files Browse the repository at this point in the history
记录springboot下的模板请求方式
  • Loading branch information
vw2019 committed Mar 15, 2019
1 parent 23fe2cf commit deacb0b
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<module>quick-wx-public</module>
<module>quick-lombok</module>
<module>quick-monitor-thread</module>
<module>quick-rest-template</module>
</modules>
<packaging>pom</packaging>
<description>使用springboot框架做的一些例子,做个记录,以后方便即拿即用</description>
Expand Down
40 changes: 40 additions & 0 deletions quick-rest-template/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<artifactId>quick-rest-template</artifactId>
<groupId>com.quick</groupId>
<version>1.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<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>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.6.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.rest.template;

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

/**
* @author vector
* @date: 2019/3/15 0015 9:19
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.rest.template.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
* @author vector
* @date: 2019/3/15 0015 9:15
*/
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
return new RestTemplate(factory);
}

@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
// 可以自定义factory
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(5000);//ms
factory.setConnectTimeout(15000);//ms
return factory;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.rest.template.controller;

import com.rest.template.model.TestDTO;
import org.springframework.web.bind.annotation.*;

/**
* 测试接口
*/
@RestController
public class TestController {

/**
* get方法测试
*
* @return
*/
@GetMapping("testGet")
public TestDTO testGet() {
TestDTO TestDTO = new TestDTO();
TestDTO.setId(1);
TestDTO.setName("get");
return TestDTO;
}

/**
* post方法
*
* @return
*/
@PostMapping("testPost")
public TestDTO testPost() {
TestDTO TestDTO = new TestDTO();
TestDTO.setId(1);
TestDTO.setName("post");
return TestDTO;
}


/**
* post 方法传值
*
* @param id
* @param name
* @return
*/
@PostMapping("testPostParam")
public String testPostParam(@RequestParam("id") String id, @RequestParam("name") String name) {
System.out.println("Post id:" + id);
System.out.println("Post name:" + name);
return "post succ";
}


/**
* post 方法传值
*
* @param id
* @param name
* @return
*/
@PutMapping("testPut")
public String testPut(@RequestParam("id") String id, @RequestParam("name") String name) {
System.out.println("put id:" + id);
System.out.println("put name:" + name);
return "del succ";
}


/**
* del方法传值
*
* @param id
* @return
*/
@DeleteMapping("testDel")
public String testDel(@RequestParam("id") String id) {
System.out.println("del id:" + id);
return "del succ";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.rest.template.model;

import lombok.Data;

/**
* @author vector
* @date: 2019/3/15 0015 9:22
*/
@Data
public class TestDTO {
private int id;
private String name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.rest.template.service;

import com.rest.template.model.TestDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.net.URISyntaxException;

/**
* @author vector
* @date: 2019/3/15 0015 9:26
*/
@Service
public class RestService {

@Autowired
private RestTemplate restTemplate;

private static String HOST = "http://localhost:8080";

private static String GET_URL = "/testGet";
private static String POST_URL = "/testPost";
private static String POST_PARAM_URL = "/testPostParam";
private static String PUT_URL = "/testPut";
private static String DEL_URL = "/testDel";


public void get() throws URISyntaxException {

ResponseEntity<TestDTO> responseEntity = this.restTemplate.getForEntity(HOST + GET_URL, TestDTO.class);
System.out.println("getForEntity: " + responseEntity.getBody());

TestDTO forObject = this.restTemplate.getForObject(HOST + GET_URL, TestDTO.class);
System.out.println("getForObject: "+ forObject);

RequestEntity<Void> requestEntity = RequestEntity.get(new URI(HOST + GET_URL)).build();
ResponseEntity<TestDTO> exchange = this.restTemplate.exchange(requestEntity, TestDTO.class);
System.out.println("exchange: "+ exchange.getBody());
}
}
2 changes: 2 additions & 0 deletions quick-rest-template/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
server:
port: 8080
29 changes: 29 additions & 0 deletions quick-rest-template/src/test/java/service/RestServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package service;

import com.rest.template.Application;
import com.rest.template.service.RestService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.net.URISyntaxException;

/**
* @author vector
* @date: 2019/3/15 0015 9:31
*/
@SpringBootTest(classes = Application.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class RestServiceTest {

@Resource
private RestService restService;

@Test
public void testGet() throws URISyntaxException {
restService.get();
}

}

0 comments on commit deacb0b

Please sign in to comment.