有没有办法获得REST Api的完整API签名在实现@Path

时间:2017-10-04 19:14:16

标签: java spring rest jax-rs spring-rest

正如标题所示,我有Spring方法实现REST和Spring REST注释@Path 我希望获得API的完整签名以获得该方法我的意思是完整的签名 例如

@Path("/customer/{id}")
method(String id){}

在那里,我可以在方法中获得完整的签名,如:http://host:port/customer/1

3 个答案:

答案 0 :(得分:2)

是的,您可以使用注释@PathParam获取{id}

@Path("/customer/{id}")
public method(@PathParam("id") String id) {
    // implementation
}    

答案 1 :(得分:0)

您只需注入UriInfo并使用方法getAbsolutePath()

@GET
@Path("/customer/{id}")
public Response get(@PathParam("id") String id, @Context UriInfo info) {
    String absPath = info.getAbsolutePath().toString();
}

顺便说一句,请花一些时间来了解Spring MVC(REST)和Jersey(JAX-RS)之间的区别。你的问题似乎暗示你认为它们可能是同一个东西,它们绝对不是。

答案 2 :(得分:0)

我认为这可能会回答你的问题。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://unpkg.com/scrollreveal/dist/scrollreveal.min.js"></script>
<body>
  
<a class="anchor" name="portfolio">
  <div class="gallery">
    <h2>Portfolio</h2>
    </a>
<div id="parent">
    <div class="img-container">
      <a href="">
        <img src="https://img1.cgtrader.com/items/4716/33d9798412/large/wicker-laundry-basket-3d-model-obj-3ds-c4d.jpg" alt=""></a>
      <div class="overlay">
          <div class="text">Basket</div>
        </div>
    </div>
    <div class="img-container">
      <a href="">
        <img src="https://cdn.shopify.com/s/files/1/0225/1115/products/buildings-rts-orc-smithy-low-poly-3d-model-2_400x.jpeg?v=1456744435" alt=""></a>
      <div class="overlay">
          <div class="text">Train</div>
        </div>
    </div>
    <div class="img-container">
      <a href="">
        <img src="http://www.pycomall.com/images/P1/Bed_Room_Interior_3D_Model_in_Max_format_7.jpg" alt=""></a>
      <div class="overlay">
          <div class="text">Bed</div>
        </div>
    </div>
    <div class="img-container">
      <a href="">
      <img src="http://mycreativedaddy.com/wp-content/uploads/chair-capitone-3d-model.jpg" alt=""></a>
      <div class="overlay">
          <div class="text">Chair</div>
        </div>
    </div>
    <div class="img-container">
      <a href="">
      <img src="http://www.pycomall.com/images/P1/Restaurant_Interior_3D_Model_in_Max_format_4.jpg" alt=""></a>
      <div class="overlay">
          <div class="text">Room</div>
        </div>
    </div>
    <div class="img-container">
      <a href="">
      <img src="https://3dexport.com/items/2008/07/29/11820/9454/wedding_ring._3d_model_c4d_max_obj_fbx_ma_lwo_3ds_3dm_stl_66996.jpg" alt=""></a>
      <div class="overlay">
          <div class="text">Ring</div>
        </div>
    </div>
    <div class="img-container">
      <a href="">
        <img src="http://www.madsonline.net/wp-content/uploads/2017/07/living-room-interior-3d-model-in-max-format-3-3d-models-in-3d-living-room-model.jpg" alt=""></a>
      <div class="overlay">
          <div class="text">Couch</div>
        </div>
    </div>
    <div class="img-container">
      <a href="">
        <img src="https://d1a9v60rjx2a4v.cloudfront.net/2013/10/03/00_54_27_730_00_1.jpg" alt=""></a>
      <div class="overlay">
          <div class="text">Glass</div>
        </div>
    </div>
    <div class="img-container">
      <a href="">
        <img src="https://img1.cgtrader.com/items/5185/bb5b9f2fde/large/bailarina-3d-model-obj-3ds-fbx-ma-mb.jpg" alt=""></a>
    </div>
</div>
  
</body>

与Path param参数一起,我们需要添加 @Path("/customer/{id}") @Get public ResponseEntity<String> getCustomer( @PathParam("id") String id, HttpServletRequest request) { System.out.println(request.getRequestURL()); return ResponseEntity.ok(id); } ,其中包含有关该特定请求的全部信息。

此处HttpServletRequest提供完整的请求网址路径。例如request.getRequestURL()

希望这有帮助。