在java代码里想要进行restful web client服务,一般使用Apache的HttpClient。不过此种方法使用起来太过繁琐。Spring Boot提供了一种简单便捷的内置模板类来进行操作,这就是RestTemplate。
Spring Boot的web starter已经内置了RestTemplate的Bean,我们主需要将它引入到我们的Spring Context中,再进行下简单的配置就可以直接使用了。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
这部分代码作为Http请求的服务端:
/**
* @desc: HTTP服务端
* @author: YanMingXin
* @create: 2022/1/15-18:08
**/
@RestController
public class RestControllerDemo {
/**
* 普通Get
*
* @param name
* @return
*/
@GetMapping("/get")
private String getMethod(@RequestParam("name") String name) {
System.out.println("getMethod : name=" + name);
return name;
}
/**
* Restful Get
*
* @param name
* @return
*/
@GetMapping("/getName/{name}")
private String getRestName(@PathVariable("name") String name) {
System.out.println("getRestName : name=" + name);
return name;
}
/**
* post
*
* @param name
* @return
*/
@PostMapping("/post")
private String postMethod(@RequestParam("name") String name) {
System.out.println("postMethod : name=" + name);
return name;
}
/**
* post json
*
* @param stu
* @return
*/
@PostMapping("/postBody")
public String postBodyMethod(@RequestBody String stu) {
Student student = JSONObject.parseObject(stu, Student.class);
System.out.println("postBodyMethod : student=" + student);
return student.toString();
}
/**
* delete
*
* @param name
* @return
*/
@DeleteMapping("/delete")
public String deleteMethod(@RequestParam("name") String name) {
System.out.println("deleteMethod : name=" + name);
return name;
}
/**
* put
*
* @param name
* @return
*/
@PutMapping("/put")
public String putMethod(@RequestParam("name") String name) {
System.out.println("putMethod : name=" + name);
return name;
}
}
配置:
/**
* @desc: RestTemplate配置
* @author: YanMingXin
* @create: 2022/1/15-17:34
**/
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
return new RestTemplate(factory);
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(5000);
factory.setConnectTimeout(15000);
return factory;
}
}
使用测试:
@SpringBootTest
class DemoApplicationTests {
@Resource
private RestTemplate restTemplate;
@Test
void getTest() {
String str = restTemplate.getForObject("http://localhost:8888/get?name=zs", String.class);
System.out.println(str);
}
@Test
void getRestTest() {
String name = "ls";
String str = restTemplate.getForObject("http://localhost:8888/getName/" + name, String.class);
System.out.println(str);
}
@Test
void postTest() {
LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.set("name", "zs");
String str = restTemplate.postForObject("http://localhost:8888/post", map, String.class);
System.out.println(str);
}
@Test
void postBodyTest() {
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers.setContentType(type);
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
HashMap<String, Object> map = new HashMap<>();
map.put("name", "zs");
map.put("age", 23);
String stu = JSON.toJSONString(map);
HttpEntity<String> formEntity = new HttpEntity<String>(stu, headers);
String str = restTemplate.postForObject("http://localhost:8888/postBody", formEntity, String.class);
System.out.println(str);
}
@Test
void putTest() {
restTemplate.put("http://localhost:8888/put?name=zs", null);
}
@Test
void deleteTest() {
restTemplate.delete("http://localhost:8888/delete?name=zs");
}
}