zoukankan      html  css  js  c++  java
  • Spring Boot Rest模板

    PUT

    通过使用RestTemplateexchange()方法来使用PUT API。

    假设此URL=> http://localhost:8080/products/3返回以下响应,使用RestTemplate来响应此API。

    下面给出的代码是请求主体 -

    //原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/spring-boot/spring_boot_rest_template.html
    {
       "name":"Huawei"
    }//原文出自【易百教程】,商业转载请联系作者获得授权,非商业请保留原文链接:https://www.yiibai.com/spring-boot/spring_boot_rest_template.html

    必须遵循以下给出的要点来使用API -

    • 自动装配Rest模板对象。
    • 使用HttpHeaders设置请求标头。
    • 使用HttpEntity包装请求对象。 在这里将Product对象包装起来以将其发送到请求主体。

    exchange()方法提供URLHttpMethodReturn类型。

    //原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/spring-boot/spring_boot_rest_template.html
    @RestController
    public class ConsumeWebService {
       @Autowired
       RestTemplate restTemplate;
    
       @RequestMapping(value = "/template/products/{id}", method = RequestMethod.PUT)
       public String updateProduct(@PathVariable("id") String id, @RequestBody Product product) {
          HttpHeaders headers = new HttpHeaders();
          headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
          HttpEntity<Product> entity = new HttpEntity<Product>(product,headers);
    
          return restTemplate.exchange(
             "http://localhost:8080/products/"+id, HttpMethod.PUT, entity, String.class).getBody();
       }
    }//原文出自【易百教程】,商业转载请联系作者获得授权,非商业请保留原文链接:https://www.yiibai.com/spring-boot/spring_boot_rest_template.html
  • 相关阅读:
    HDU 5818 Joint Stacks (优先队列)
    POJ 3169 Layout (差分约束系统)
    HDU 4370 0 or 1 (最短路+最小环)
    LightOJ 1074 Extended Traffic (最短路spfa+标记负环点)
    HDU 1142 A Walk Through the Forest (求最短路条数)
    力扣71——简化路径
    力扣73——矩阵置零
    ThreadLocal的进化——TransmittableThreadLocal
    ThreadLocal的进化——InheritableThreadLocal
    Java——内部类详解
  • 原文地址:https://www.cnblogs.com/0710whh/p/12391081.html
Copyright © 2011-2022 走看看