ResetTemplate 使用样例
说明: 具体的注意事项已经在放在了注释中了,请读者自行查看
@SpringBootTest
class RestApplicationTests {
@Autowired
private RestTemplate restTemplate;
private String username = "weijinhao";
private String password = "123456";
/**
* 测试get方法,需要注意的是光有后面的param是不够的,需要我们在url上拼接上占位符
*/
@Test
void getMethodTest() {
Map<String, String> param = new HashMap<>();
param.put("username",username);
param.put("password",password);
String forObject = restTemplate.
getForObject("http://localhost:8080/getMethod?username={username}&password={password}", String.class, param);
assert (username + password) .equals(forObject);
}
/**
* 表单提交测试
*
*/
@Test
void postBodyMethodTest() {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
param.add("username",username);
param.add("password",password);
HttpEntity<MultiValueMap<String, Object> > request = new HttpEntity<>(param,httpHeaders);
String ret = restTemplate.postForObject("http://localhost:8080/postBodyMethod", request, String.class, (Object) null);
assert ret.equals(username + password);
}
/**
* json 请求
*/
@Test
public void jsonMethodTest() {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> request = new HttpEntity<>("{\"username\":\"weijinhao\",\"password\":\"123456\"}",httpHeaders);
String ret = restTemplate.postForObject("http://localhost:8080/jsonMethod", request, String.class, (Object) null);
assert ret.equals(username + password);
}
/**
* 上传文件请求,注意点是java io 中的 inputStream 没有实现序列化接口,所以在序列化的时候会报错,我们在这使用 resource 的实现类FileSystemResource
*/
@Test
public void fileTest() {
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("username",username);
body.add("password",password);
body.add("file", new FileSystemResource("C:\\\\Users\\\\Administrator\\\\Desktop\\\\微信图片_20201215211126.jpg"));
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(body,httpHeaders);
String s = restTemplate.postForObject("http://localhost:8080/fileMethod", request, String.class, (Object) null);
assert (username + password).equals(s);
}
}
对应的controller请求格式
@RestController
public class Controller {
/**
* paramter
*/
@GetMapping("/getMethod")
public String getMethod(@RequestParam("username") String username,@RequestParam("password") String password) {
System.out.println(username);
System.out.println(password);
return username + password;
}
/**
* 表单 需要注意的是 不能使用@requestBody注解,springmvc框架会为我们自动发现你
*/
@PostMapping("/postMethod")
public String postMethod(@RequestParam("username") String username,@RequestParam("password") String password) {
System.out.println(username);
System.out.println(password);
return username + password;
}
/**
* 表单接受body
*/
@PostMapping("/postBodyMethod")
public String postBodyMethod(User user) {
System.out.println(user.getUsername());
System.out.println(user.getPassword());
return user.getUsername() + user.getPassword();
}
/**
* 文件上传
*/
@PostMapping("/fileMethod")
public String fileMethod(@RequestParam("username") String username, @RequestParam("password") String password,
@RequestParam("file") MultipartFile file) throws IOException {
System.out.println(username);
System.out.println(password);
FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\20201215211126.jpg",false);
IOUtils.copy(file.getInputStream(),fileOutputStream);
fileOutputStream.close();
return username + password;
}
/**
* json body
*/
@PostMapping("/jsonMethod")
public String jsonMethod(@RequestBody User user) {
System.out.println(user.getUsername());
System.out.println(user.getPassword());
return user.getUsername() + user.getPassword();
}
}