java - 如何指定 Jersey 中属性文件的路径值?

标签 java jersey jersey-2.0 jersey-1.0

我希望路径不应该被硬编码,而是从属性中获取,以便我们可以根据需要更改它。

下面的代码有效:---

@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...
        */
    }

关于java - 如何指定 Jersey 中属性文件的路径值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27002286/

相关文章:

java - (Project Tango)通过区域学习进行点云的旋转和平移

java - 如何在我的 Gradle 项目中使用 slf4j 在 IntelliJ 中获取日志记录输出?

jersey - 为什么对 @RequiresUser 的请求没有重定向到登录页面?

java - 保护 RESTful Web 服务(Tomcat 上的泽西)

java - Jersey Server-Sent Events - 写入断开的连接不会抛出异常

java - java中变量的命名限制

java - java中的sleep方法是如何工作的

java - 为什么 Jersey 中的 XmlRootElementJaxbProvider 使用 SAXSource

rest - 在 JAX-RS 中不会对 Null 值调用 ParamConverter 的 fromString 方法

jakarta-ee - 具有多种功能的 Jersey 网络服务