六种请求方式
实体类
User
import lombok.AllArgsConstructor;
import lombok.Data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@Data
@AllArgsConstructor
public class User implements Serializable {
private String name;
private int age;
private String email;
private Dog dog;
private User(){
}
public static List<User> creatUser(){
List<User> users = new ArrayList();
users.add(new User("李四",1,"114@qq.com",new Dog("旺财",1)));
users.add(new User("张三",1,"124@qq.com",new Dog("狗子",1)));
return users;
}
}
Dog
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dog implements Serializable {
private String name;
private int age;
}
client
GET
@Test
void contextLoads() {
RestTemplate restTemplate = new RestTemplate();
System.out.println("---------------");
// System.out.println(restTemplate.getForObject("http://localhost:8081/query", new ArrayList<User>().getClass(),1,2));//1,2路径变量
// System.out.println(restTemplate.getForObject("http://localhost:8081/query", new ArrayList<User>().getClass()));
ResponseEntity<User> resp =restTemplate.getForEntity("http://localhost:8081/queryOne", User.class);
System.out.println(resp.getStatusCode());
System.out.println(resp.getStatusCodeValue());
System.out.println(resp.getBody());
System.out.println(resp.getHeaders());
System.out.println("---------------");
}
POST
@Test
void postTest(){
RestTemplate restTemplate = new RestTemplate();
System.out.println("---------------");
String url = "http://localhost:8081/inset";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
User user=new User("李四",10,"1149889010@qq.com",new Dog("ww",2));
HttpEntity<User> request = new HttpEntity<>(user, headers);
ResponseEntity<String> response = restTemplate.postForEntity( url, request , String.class );
System.out.println(response.getBody());
System.out.println("---------------");
}
server
List<User> users=User.creatUser();
@PostMapping("/inset")
public String inset(@RequestBody User user){
System.out.println(user.toString());
users.add(user);
return "成功:200";
}
@GetMapping("/query")
public List<User> query(){
return users;
}
@GetMapping("/queryOne")
public User queryOne(){
return users.get(0);
}
设置超时时间
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(5000);
requestFactory.setReadTimeout(5000);
RestTemplate restTemplate = new RestTemplate(requestFactory);
return restTemplate;
}
}