zoukankan      html  css  js  c++  java
  • UriComponentsBuilder和UriComponents url编码

    Spring MVC 提供了一种机制,可以构造和编码URI -- 使用UriComponentsBuilder和UriComponents。

    功能相当于 urlencode()函数,对url进行编码, 但同时还支持变量替换

    UriComponents uriComponents = UriComponentsBuilder.fromUriString(
            "http://example.com/hotels/{hotel}/bookings/{booking}").build();
    
    URI uri = uriComponents.expand("42", "21").encode().toUri();
    

      

    嗯,expand()是用于替换所有的模板变量,encode默认使用UTF8编码。

    注意,UriComponents是不可变的,expand()和encode()都是返回新的实例。

    你还可以这样做:

    UriComponents uriComponents = UriComponentsBuilder.newInstance()
            .scheme("http").host("example.com").path("/hotels/{hotel}/bookings/{booking}").build()
            .expand("42", "21")
            .encode();
    

      

    在Servlet环境中,使用子类ServletUriComponentsBuilder提供的静态工厂方法可以从一个Servlet request中获取有用的URI信息:

    HttpServletRequest request = ...
    
    // Re-use host, scheme, port, path and query string
    // Replace the "accountId" query param
    
    ServletUriComponentsBuilder ucb = ServletUriComponentsBuilder.fromRequest(request)
            .replaceQueryParam("accountId", "{id}").build()
            .expand("123")
            .encode();
    

      

    https://www.cnblogs.com/larryzeal/p/6131664.html

  • 相关阅读:
    C++字符串以及转换整理
    Chromium 调试其他的进程
    Cstring和wstring互转
    注册表写入
    inno setup需要管理员权限
    C++ 新增的算法
    节选-文件描述符
    原创-docker镜像迁移另外仓库
    2021初赛:毒瘤汇总
    二维计算几何基础
  • 原文地址:https://www.cnblogs.com/achengmu/p/9145647.html
Copyright © 2011-2022 走看看