• Spring RestTemplate 之post请求


    ●post请求:在RestTemplate中,POST请求可以通过如下三个方法来发起,但post提交方式又有两种 formData 和 payLoad,而且接口设计与传统的浏览器使用的提交方式又有差异.所以很容易产生混淆。

    formData和payLoad的区别:

       当POST请求的请求头里设置Content-Type: application/x-www-form-urlencoded(默认), 参数在请求体以标准的Form Data的形式提交,以&符号拼接,参数格式为key=value&key=value&key=value…. 

        如果使用AJAX原生POST请求,请求头里设置Content-Type:application/json,请求的参数会显示在Request Payload中,参数格式为JSON格式:{“key”:”value”,”key”:”value”…},这种方式可读性会更好。

     第一种:postForEntity

            @RequestMapping("/hello5")

    public String getHello5() throws Exception {

    Book book = new Book();

    book.setName("水浒传");

    book.setPrice("200");

    book.setAuthor("xxx");

    book.setPublisher("xxxxxxxxxxxxx");

    HttpHeaders headers = new HttpHeaders();

                    //这里设置的是以payLoad方式提交数据,对于Payload方式,提交的内容一定要是String,且Header要设为“application/json”

    headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

    ObjectMapper mapper = new ObjectMapper();

    String value = mapper.writeValueAsString(book);

    HttpEntity<String> requestEntity = new HttpEntity<String>(value,

    headers);

    ResponseEntity<String> res = restTemplate.postForEntity(

    "http://10.145.198.143:8081/ords/data_service/monitor/books",

    requestEntity, String.class);

    return res.getBody();

    }

         下面是以formData方式请求:

        @RequestMapping("hello6")
        public String hello6() throws JsonProcessingException{
            //ResponseEntity<String> res = restTemplate.getForEntity("http://localhost/test/hello2", String.class);
            HttpHeaders header = new HttpHeaders();
            header.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            User u = new User();
            u.setName("rdb");
            u.setPasswd("abc123");
            ObjectMapper om = new ObjectMapper();
            String value = om.writeValueAsString(u);
            System.out.println(u.toString());
            System.out.println(value);
            HttpEntity<String> entity = new HttpEntity<String>(value,header);
            ResponseEntity<String> res = restTemplate.postForEntity("http://localhost/test/hello2", entity, String.class);
            return res.getBody();
        }

    服务端接收参数总结说明:

        @RequestParam :

        A) 常用来处理简单类型的绑定,通过Request.getParameter() 获取的String可直接转换为简单类型的情况( 由String到 简单类型的转换操作由ConversionService配置的转换器来完成);因为使用request.getParameter()方式获取参数,所以可以处理get 方式中queryString的值,也可以处理post方式中 body data的值。
        B)用来处理Content-Type: 为 application/x-www-form-urlencoded编码的内容,提交方式GET、POST。(不设置这个属性,好像这就是默认值)
        C) 该注解有两个属性: value、required; value用来指定要传入值的id名称,required用来指示参数是否必须绑定。

        在方法参数里面如是:public @ResponseBody JsonResult getPublishedToposByConnStreamId(@RequestParam(value = "streamId", required = false) String streamId) {}       

        @RequestBody
        A) GET、POST方式提时, 根据request header Content-Type的值来判断:
        application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的数据@RequestParam, @ModelAttribute也可以处理,当然@RequestBody也能处理);
        multipart/form-data, 不能处理(次类型多用来上传文件类型---即使用@RequestBody不能处理这种格式的数据,@RequestParam这个却是可以处理的。);
        其他格式, 必须(其他格式包括application/json, application/xml等。这些格式的数据,必须使用@RequestBody来处理);

        B) PUT方式提交时, 根据request header Content-Type的值来判断:(表示没见过put方式滴,可以无视吧。)
        application/x-www-form-urlencoded, 必须;
        multipart/form-data, 不能处理;
        其他格式, 必须;

    结论:@RequestBody这个一般处理的是在ajax请求中声明contentType: "application/json; charset=utf-8"时候。也就是json数据或者xml(我没用过这个,用的是json)

               @RequestParam这个一般就是在ajax里面没有声明contentType的时候,为默认的APPLICATION_FORM_URLENCODED格式时,用这个。

    第二种:postForObject

    如果你只关注,返回的消息体,可以直接使用postForObject。用法和getForObject一致。

    第三种:postForLocation

    postForLocation也是提交新资源,提交成功之后,返回新资源的URI,postForLocation的参数和前面两种的参数基本一致,只不过该方法的返回值为Uri,这个只需要服务提供者返回一个Uri即可,该Uri表示新资源的位置。

  • 相关阅读:
    opencv计算机视觉学习笔记一
    opencv_python学习笔记十八
    opencv_python学习笔记十六
    Android-锁屏功能
    Android-加载透明PNG图片变黑的问题
    Android-AttributeSet详解
    Android-UGallery
    Android-相册效果(图片缩放 自由滑动)
    Android进阶篇-内存管理
    Android进阶篇-时间滑动控件
  • 原文地址:https://www.cnblogs.com/jnba/p/10522565.html
Copyright © 2020-2023  润新知