【问题标题】:How to get the url of called method resteasy如何获取被调用方法resteasy的url
【发布时间】:2015-03-19 15:35:04
【问题描述】:

我使用 Restaeasy (java) 制作了一项 Rest 服务,该服务必须返回被调用的相同 URL,但带有一个新字符串

示例调用服务:

Post => mybase/myservice/somewrite 和一些 JSON
|响应 => mybase/myservice/somewrite/123456

所以我想用一个通用逻辑制作 mybase/myservice/somewrite url,因为如果我输入 String returnURL="mybase/myservice/somewrite"; 并且我更改了例如 mybase 的名称,则响应将不会是不错

我想要这样的东西

someLogicService(JSON);
id=getId();
URL=getContextCallURL();
return URL+\/+id;

但我不知道这是否可以做到,更不用说如何做到了

【问题讨论】:

    标签: rest jakarta-ee resteasy


    【解决方案1】:

    您还可以在资源中使用注解Context 注入UriInfo 类型的实例,如下所述:

    @Context
    private UriInfo uriInfo;
    
    @POST
    @Path("/")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response makeContact(Contact contact)  {
        String requestUri = uriInfo.getRequestUri();
        (...)
    }
    

    希望对你有帮助 蒂埃里

    【讨论】:

      【解决方案2】:

      我找到了问题的答案,我用@context 将 httpRequest 注入我的函数并调用 absolutPath :

       @POST
       @Path("/")
       @Consumes(MediaType.APPLICATION_JSON)
       public Response makeContact(Contact contact, @Context HttpRequest request)  {
              return Response.ok().header("location", request.getUri().getAbsolutePath().getPath() + contactService.makeContact(contactJSON)).build();
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-10
        相关资源
        最近更新 更多