ResetTemplate 使用样例

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();
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值