forked from vector4wang/spring-boot-quick
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(quick-rest-template): resttemplate
记录springboot下的模板请求方式
- Loading branch information
Showing
9 changed files
with
254 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
16 changes: 16 additions & 0 deletions
16
quick-rest-template/src/main/java/com/rest/template/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
28 changes: 28 additions & 0 deletions
28
quick-rest-template/src/main/java/com/rest/template/config/RestTemplateConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
quick-rest-template/src/main/java/com/rest/template/controller/TestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
quick-rest-template/src/main/java/com/rest/template/model/TestDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
44 changes: 44 additions & 0 deletions
44
quick-rest-template/src/main/java/com/rest/template/service/RestService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
server: | ||
port: 8080 |
29 changes: 29 additions & 0 deletions
29
quick-rest-template/src/test/java/service/RestServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |