簡體   English   中英

如何從jersey的屬性文件中指定路徑值?

[英]How can i specify path value from property file in jersey?

我希望路徑不應該硬編碼,而應該從屬性中選取,以便我們可以根據需要進行更改。

以下代碼有效:---

@Path("ws/{version}")
public class DesignationResource {

  @PathParam("version") String version = 
              Constants.API_VERSION; //(read from property file in class Constants)
  @PathParam("servicename_designationList") String servicename_designationList=
              Constants.API_POST_CITYLIST_NAME ; //(read from property file in class Constants)



  @Path("{servicename_designationList}")
  @Produces(MediaType.APPLICATION_JSON)
    public Response getDesignations()
    {
       /**
         ...CODES...
        */
    }
} 

但是,如果類有兩個方法,則它不起作用並拋出異常

代碼:---

@Path("ws/{version}")
public class DesignationResource {

  @PathParam("version") String version = 
              Constants.API_VERSION; //(read from property file in class Constants)
  @PathParam("servicename_designationList") String servicename_designationList=
              Constants.API_POST_CITYLIST_NAME ; //(read from property file in class Constants)
  @PathParam("servicename_designationListId") String servicename_designationListId=
              Constants.API_POST_CITYLISTID_NAME ; //(read from property file in class Constants)



  @Path("{servicename_designationList}")
  @Produces(MediaType.APPLICATION_JSON)
    public Response getDesignations()
    {
       /**
         ...CODES...
        */
    }

  @Path("{servicename_designationListId}")
  @Produces(MediaType.APPLICATION_JSON)
    public Response getDesignationsId()
    {
       /**
         ...CODES...
        */
    }
} 

異常記錄為:-----

org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.  
[[FATAL] A resource model has ambiguous (sub-)resource method for HTTP method GET and input mime-types as defined by @Consumes and @Produces annotations at Java methods public javax.ws.rs.core.Response DesignationResource.getDesignations() and public javax.ws.rs.core.Response DesignationResource.getDesignationsId() at matching regular expression /([^/]+?). These two methods produces and consumes exactly the same mime-types and therefore their invocation as a resource methods will always fail.; source='org.glassfish.jersey.server.model.RuntimeResource@7e5ba613', [FATAL] A resource model has ambiguous (sub-)resource method for HTTP method GET and input mime-types as defined by @Consumes and @Produces annotations at Java methods public javax.ws.rs.core.Response source='org.glassfish.jersey.server.model.RuntimeResource@7e5ba613']  
  at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:465)  
  ...

您在方法中使用相同的路徑URL(servicename_designationListId)。 為您的方法提供不同的路徑,如下所示。

@Path("{servicename_designations}")
  @Produces(MediaType.APPLICATION_JSON)
    public Response getDesignations()
    {
       /**
         ...CODES...
        */
    }

  @Path("{servicename_designationListId}")
  @Produces(MediaType.APPLICATION_JSON)
    public Response getDesignationsId()
    {
       /**
         ...CODES...
        */
    }

正如stacktrace所說,路徑必須是唯一的(或者您使用不同的媒體類型)。 我認為,您想執行以下操作:

@Path( Constants.API_POST_CITYLIST_NAME )
@Produces( MediaType.APPLICATION_JSON )
public Response getDesignations()
{
   /**
     ...CODES...
    */
}

@Path( Constants.API_POST_CITYLISTID_NAME )
@Produces( MediaType.APPLICATION_JSON )
public Response getDesignationsId()
{
   /**
     ...CODES...
    */
}

使用程序化API來注冊資源。 它將允許您以無法通過注釋管理的方式在運行時動態注冊事物。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM