繁体   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