有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将URI变量值传递到url

原谅新手的问题--

如何在url中传递uri变量的值

(这适用于进行REST调用的spring boot应用程序)

例如,如果我有url:

"http://example.com/hotels/{hotel}/bookings/{booking}" 

假设“hotel”和“booking”有字符串值,我如何传递这些值


共 (2) 个答案

  1. # 1 楼答案

    简单,假设一个{hotel}被称为“棺材”{booking}有“蓝色”的价值

    因此,您的路由:"http://example.com/hotels/{hotel}/bookings/{booking}",应该如下所示:

    "http://example.com/hotels/Casket/bookings/Blue" 
    

    您所需要做的就是调用该URL并读取服务器给您的响应

  2. # 2 楼答案

    import org.springframework.web.util.UriTemplate;
    import java.net.URI;
    
    UriTemplate uritemplate= new UriTemplate("http://example.com/hotels/{hotel}/bookings/{booking}");
    URI uri = tmp.expand(hotel, booking);
    

    这个问题的好答案令人惊讶地难以找到

    Spring doc有一些有用的例子